gdp-wired 0.31.1 → 0.33.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +95 -6
- package/dist/core/c/reader.d.ts +86 -0
- package/dist/core/c/reader.js +860 -0
- package/dist/core/check.js +20 -6
- package/dist/core/cli.d.ts +76 -0
- package/dist/core/cli.js +636 -0
- package/dist/core/dsl.d.ts +6 -4
- package/dist/core/dsl.js +30 -16
- package/dist/core/effect.d.ts +6 -4
- package/dist/core/effect.js +48 -10
- package/dist/core/ex/harness.exs +177 -0
- package/dist/core/ex/json.exs +46 -0
- package/dist/core/ex/reader.exs +462 -0
- package/dist/core/exists.d.ts +3 -0
- package/dist/core/exists.js +19 -5
- package/dist/core/extract-c.d.ts +35 -0
- package/dist/core/extract-c.js +424 -0
- package/dist/core/extract-check.js +27 -6
- package/dist/core/extract-ex.d.ts +16 -0
- package/dist/core/extract-ex.js +33 -0
- package/dist/core/extract-go-check.d.ts +28 -0
- package/dist/core/extract-go-check.js +639 -0
- package/dist/core/extract-go.d.ts +231 -0
- package/dist/core/extract-go.js +1166 -0
- package/dist/core/extract-jvm.d.ts +23 -0
- package/dist/core/extract-jvm.js +220 -0
- package/dist/core/extract-print.js +17 -7
- package/dist/core/extract-py.d.ts +15 -0
- package/dist/core/extract-py.js +31 -0
- package/dist/core/extract-rb.d.ts +17 -0
- package/dist/core/extract-rb.js +31 -0
- package/dist/core/extract-read.d.ts +20 -0
- package/dist/core/extract-read.js +46 -6
- package/dist/core/extract-rs.d.ts +12 -0
- package/dist/core/extract-rs.js +306 -0
- package/dist/core/extract-shapes.d.ts +2 -0
- package/dist/core/extract-shapes.js +6 -5
- package/dist/core/extract-step.d.ts +806 -0
- package/dist/core/extract-step.js +4141 -0
- package/dist/core/extract-swift.d.ts +27 -0
- package/dist/core/extract-swift.js +484 -0
- package/dist/core/extract-xstate.d.ts +10 -3
- package/dist/core/extract-xstate.js +50 -4
- package/dist/core/extract.d.ts +22 -3
- package/dist/core/extract.js +107 -18
- package/dist/core/go/go.mod +3 -0
- package/dist/core/go/reader.go +901 -0
- package/dist/core/infer.d.ts +4 -0
- package/dist/core/infer.js +1838 -69
- package/dist/core/jvm/Harness.java +361 -0
- package/dist/core/jvm/JavaReader.java +825 -0
- package/dist/core/jvm/Jvm.java +332 -0
- package/dist/core/jvm/KtReader.java +960 -0
- package/dist/core/jvm/Rt.java +10 -0
- package/dist/core/laws.d.ts +83 -0
- package/dist/core/laws.js +623 -0
- package/dist/core/machine.d.ts +2 -0
- package/dist/core/machine.js +87 -0
- package/dist/core/py/harness.py +328 -0
- package/dist/core/py/reader.py +447 -0
- package/dist/core/rb/harness.rb +233 -0
- package/dist/core/rb/reader.rb +375 -0
- package/dist/core/relational.d.ts +49 -0
- package/dist/core/relational.js +345 -0
- package/dist/core/rs/Cargo.lock +108 -0
- package/dist/core/rs/Cargo.toml +14 -0
- package/dist/core/rs/src/main.rs +681 -0
- package/dist/core/swift/Harness.swift +124 -0
- package/dist/core/swift/Main.swift +34 -0
- package/dist/core/swift/Package.swift +13 -0
- package/dist/core/swift/Sources/gdp-swiftreader/main.swift +1341 -0
- package/dist/interchange/check.d.ts +16 -0
- package/dist/interchange/check.js +111 -0
- package/dist/interchange/format.d.ts +150 -0
- package/dist/interchange/format.js +678 -0
- package/dist/kernel/epr.d.ts +40 -0
- package/dist/kernel/epr.js +1882 -0
- package/dist/kernel/kernel.d.ts +45 -1
- package/dist/kernel/kernel.js +138 -13
- package/dist/kernel/prove.d.ts +45 -0
- package/dist/kernel/prove.js +658 -51
- package/dist/lean-kernel/emit.d.ts +17 -2
- package/dist/lean-kernel/emit.js +87 -7
- package/package.json +57 -2
|
@@ -0,0 +1,960 @@
|
|
|
1
|
+
// The extractor's Kotlin reader (core/extract-jvm.ts): a class read through the Kotlin compiler's own parser (its PSI,
|
|
2
|
+
// from the kotlin-compiler.jar of the kotlinc installed) and lowered into the shared step IR through the JVM layer the
|
|
3
|
+
// Java reader uses (core/jvm/Jvm.java: one type per JVM type, the library's calls lowered alike). The PSI is syntax:
|
|
4
|
+
// the types are the code's own declarations (Kotlin states a property's and a parameter's), a local's from its
|
|
5
|
+
// initializer, an expression's from its parts. Kotlin's semantics as the IR's operations: Int and Long wrap, a nullable
|
|
6
|
+
// type is an option (?:, !!, ?.let, smart casts), when is a chain of ifs, if and when as values are pushed into their
|
|
7
|
+
// branches where they return, the standard library's scope functions (let, also, run, apply, takeIf, takeUnless) are
|
|
8
|
+
// inlined, check/require/error throw, @Synchronized, synchronized and withLock make the step atomic, a data class's
|
|
9
|
+
// copy is an update and a method of an immutable class returning its next value is a step to it.
|
|
10
|
+
//
|
|
11
|
+
// java -cp <tools>:<kotlin-compiler.jar>:<kotlin-stdlib.jar> gdp.jvm.KtReader '{"root": <dir>, "file": <file>, "names": ["Class"]}'
|
|
12
|
+
package gdp.jvm;
|
|
13
|
+
|
|
14
|
+
import com.intellij.openapi.util.Disposer;
|
|
15
|
+
import com.intellij.psi.PsiElement;
|
|
16
|
+
import com.intellij.psi.tree.IElementType;
|
|
17
|
+
import java.io.File;
|
|
18
|
+
import java.nio.file.*;
|
|
19
|
+
import java.util.*;
|
|
20
|
+
import org.jetbrains.kotlin.KtNodeTypes;
|
|
21
|
+
import org.jetbrains.kotlin.cli.jvm.compiler.*;
|
|
22
|
+
import org.jetbrains.kotlin.config.*;
|
|
23
|
+
import org.jetbrains.kotlin.lexer.KtTokens;
|
|
24
|
+
import org.jetbrains.kotlin.psi.*;
|
|
25
|
+
import static gdp.jvm.Jvm.*;
|
|
26
|
+
|
|
27
|
+
public final class KtReader implements Jvm.Env {
|
|
28
|
+
record X(Map<String, Object> ir, JT t) {}
|
|
29
|
+
// A lowering's context: the statements an expression needs before it (a scope function's), the locals' types, the
|
|
30
|
+
// names the lambda's `it` and `this` stand for.
|
|
31
|
+
static final class Ctx {
|
|
32
|
+
final List<Object> pre; final Map<String, JT> locals; final int cond; final boolean init;
|
|
33
|
+
Ctx(List<Object> pre, Map<String, JT> locals, int cond, boolean init) { this.pre = pre; this.locals = locals; this.cond = cond; this.init = init; }
|
|
34
|
+
Ctx under() { return new Ctx(pre, locals, cond + 1, init); }
|
|
35
|
+
Ctx fresh() { return new Ctx(new ArrayList<>(), locals, cond, init); }
|
|
36
|
+
Ctx scope() { return new Ctx(pre, new HashMap<>(locals), cond, init); }
|
|
37
|
+
}
|
|
38
|
+
KtFile file; String src, base, pkg;
|
|
39
|
+
int[] lines;
|
|
40
|
+
final Map<String, KtClassOrObject> classes = new LinkedHashMap<>();
|
|
41
|
+
final Map<String, String> imports = new HashMap<>();
|
|
42
|
+
final List<String> stars = new ArrayList<>(List.of("kotlin.", "kotlin.collections.", "kotlin.comparisons.", "kotlin.ranges.", "kotlin.text.", "java.lang.", "kotlin.jvm."));
|
|
43
|
+
final Map<String, Object> recs = new LinkedHashMap<>(), funcs = new LinkedHashMap<>(), jvm = new LinkedHashMap<>();
|
|
44
|
+
final List<Object> rewrites = new ArrayList<>();
|
|
45
|
+
final List<String> reducers = new ArrayList<>();
|
|
46
|
+
KtClassOrObject self;
|
|
47
|
+
String selfName;
|
|
48
|
+
TreeMap<Integer, String> tags;
|
|
49
|
+
int tmp = 0;
|
|
50
|
+
|
|
51
|
+
public static void main(String[] argv) throws Exception {
|
|
52
|
+
Map<?, ?> req = (Map<?, ?>) parse(argv[0]);
|
|
53
|
+
Jvm.kotlin = true;
|
|
54
|
+
System.out.println(json(new KtReader().read(req)));
|
|
55
|
+
}
|
|
56
|
+
static KtPsiFactory factory() throws Exception {
|
|
57
|
+
CompilerConfiguration cfg = new CompilerConfiguration();
|
|
58
|
+
cfg.put(CommonConfigurationKeys.MESSAGE_COLLECTOR_KEY, org.jetbrains.kotlin.cli.common.messages.MessageCollector.Companion.getNONE());
|
|
59
|
+
// (Kotlin 2.2 and later want an extension storage registered; earlier ones have no such key)
|
|
60
|
+
try {
|
|
61
|
+
Class<?> k = Class.forName("org.jetbrains.kotlin.cli.FrontendConfigurationKeysKt"), st = Class.forName("org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar$ExtensionStorage");
|
|
62
|
+
k.getMethod("setExtensionsStorage", CompilerConfiguration.class, st).invoke(null, cfg, st.getConstructor().newInstance());
|
|
63
|
+
} catch (ClassNotFoundException | NoSuchMethodException e) { /* an older compiler */ }
|
|
64
|
+
KotlinCoreEnvironment env = KotlinCoreEnvironment.createForProduction(Disposer.newDisposable(), cfg, EnvironmentConfigFiles.JVM_CONFIG_FILES);
|
|
65
|
+
return new KtPsiFactory(env.getProject(), false);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
Map<String, Object> read(Map<?, ?> req) throws Exception {
|
|
69
|
+
String path = (String) req.get("file"), root = (String) req.get("root");
|
|
70
|
+
src = Files.readString(Path.of(path));
|
|
71
|
+
base = new File(path).getName();
|
|
72
|
+
KtPsiFactory f = factory();
|
|
73
|
+
file = f.createFile(base, src);
|
|
74
|
+
pkg = file.getPackageFqName().asString();
|
|
75
|
+
List<Integer> ls = new ArrayList<>(List.of(0));
|
|
76
|
+
for (int i = 0; i < src.length(); i++) if (src.charAt(i) == '\n') ls.add(i + 1);
|
|
77
|
+
lines = ls.stream().mapToInt(Integer::intValue).toArray();
|
|
78
|
+
tags = tags(src);
|
|
79
|
+
for (KtImportDirective im : file.getImportDirectives()) {
|
|
80
|
+
if (im.getImportedFqName() == null) continue;
|
|
81
|
+
String q = im.getImportedFqName().asString();
|
|
82
|
+
if (im.isAllUnder()) stars.add(q + ".");
|
|
83
|
+
else imports.put(im.getAliasName() != null ? im.getAliasName() : q.substring(q.lastIndexOf('.') + 1), q);
|
|
84
|
+
}
|
|
85
|
+
index(file.getDeclarations(), pkg.isEmpty() ? "" : pkg + ".", "");
|
|
86
|
+
// (the other files of the file's directory: their classes and enums, by name)
|
|
87
|
+
File[] sibs = new File(path).getAbsoluteFile().getParentFile().listFiles((d, n) -> n.endsWith(".kt") && !n.equals(base));
|
|
88
|
+
for (File s : sibs == null ? new File[0] : sibs) { try { KtFile o = f.createFile(s.getName(), Files.readString(s.toPath())); String op = o.getPackageFqName().asString(); index(o.getDeclarations(), op.isEmpty() ? "" : op + ".", ""); } catch (Exception e) { /* unread */ } }
|
|
89
|
+
Map<String, Object> out = m("lang", "kotlin", "file", base, "path", new File(path).getAbsolutePath(), "module", pkg, "root", new File(root).getAbsolutePath());
|
|
90
|
+
List<Object> entries = new ArrayList<>();
|
|
91
|
+
List<?> names = (List<?>) req.get("names");
|
|
92
|
+
// (no names: every class of the file, the executor taking the one whose public methods write its state)
|
|
93
|
+
if (names.isEmpty()) { List<Object> all = new ArrayList<>(); for (Map.Entry<String, KtClassOrObject> e : classes.entrySet()) if (e.getValue().getContainingFile() == file && e.getValue() instanceof KtClass k && !k.isInterface() && !k.isEnum() && !k.isInner() && !k.isSealed() && !k.hasModifier(KtTokens.ABSTRACT_KEYWORD)) all.add(e.getKey().replace('$', '.')); names = all; }
|
|
94
|
+
for (Object n : names) {
|
|
95
|
+
String key = null;
|
|
96
|
+
for (String k : classes.keySet()) { String dotted = k.replace('$', '.'); if (dotted.equals(n) || dotted.endsWith("." + n)) { key = k; break; } }
|
|
97
|
+
entries.add(key == null ? m("k", "missing", "name", n) : m("k", "class", "rec", entry(key)));
|
|
98
|
+
}
|
|
99
|
+
out.put("entries", entries); out.put("recs", recs); out.put("funcs", funcs);
|
|
100
|
+
Map<Object, Object> once = new LinkedHashMap<>();
|
|
101
|
+
for (Object r : rewrites) once.putIfAbsent(((Map<?, ?>) r).get("span"), r);
|
|
102
|
+
jvm.put("rewrites", new ArrayList<>(once.values())); jvm.put("reducers", reducers);
|
|
103
|
+
out.put("jvm", jvm);
|
|
104
|
+
return out;
|
|
105
|
+
}
|
|
106
|
+
void index(List<KtDeclaration> ds, String prefix, String outer) {
|
|
107
|
+
for (KtDeclaration d : ds) if (d instanceof KtClassOrObject c && c.getName() != null) {
|
|
108
|
+
String bin = prefix + outer + c.getName();
|
|
109
|
+
classes.putIfAbsent(bin, c);
|
|
110
|
+
if (c.getBody() != null) index(c.getBody().getDeclarations(), prefix, outer + c.getName() + "$");
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
String binOf(KtClassOrObject c) { for (Map.Entry<String, KtClassOrObject> e : classes.entrySet()) if (e.getValue() == c) return e.getKey(); return c.getName(); }
|
|
114
|
+
|
|
115
|
+
// --- positions ---------------------------------------------------------------------------------------------------------
|
|
116
|
+
int line(int off) { int i = Arrays.binarySearch(lines, off); return i >= 0 ? i + 1 : -i - 1; }
|
|
117
|
+
String where(PsiElement e) { int o = e.getTextRange().getStartOffset(), l = line(o); return base + ":" + l + ":" + (o - lines[l - 1] + 1); }
|
|
118
|
+
String zero() { return base + ":0:0"; }
|
|
119
|
+
static String text(PsiElement e) { String x = e.getText().replaceAll("\\s+", " "); return x.length() <= 60 ? x : x.substring(0, 57) + "..."; }
|
|
120
|
+
List<Object> span(PsiElement e) { return List.of(e.getTextRange().getStartOffset(), e.getTextRange().getEndOffset()); }
|
|
121
|
+
String tag(PsiElement e) { int l0 = line(e.getTextRange().getStartOffset()), l1 = line(e.getTextRange().getEndOffset()); var h = tags.subMap(l0, true, Math.max(l0, Math.min(l1, l0 + 1)), true); return h.isEmpty() ? null : String.join("; ", h.values()); }
|
|
122
|
+
|
|
123
|
+
// --- types ---------------------------------------------------------------------------------------------------------
|
|
124
|
+
static final Map<String, String> BUILTIN = Map.ofEntries(Map.entry("Int", "int"), Map.entry("Long", "long"), Map.entry("Short", "short"), Map.entry("Byte", "byte"), Map.entry("Char", "char"), Map.entry("Boolean", "boolean"), Map.entry("Float", "float"), Map.entry("Double", "double"),
|
|
125
|
+
Map.entry("String", "java.lang.String"), Map.entry("CharSequence", "java.lang.CharSequence"), Map.entry("Any", "java.lang.Object"), Map.entry("Unit", "void"), Map.entry("Nothing", "void"), Map.entry("Number", "java.lang.Number"),
|
|
126
|
+
Map.entry("Map", "java.util.Map"), Map.entry("MutableMap", "java.util.Map"), Map.entry("HashMap", "java.util.HashMap"), Map.entry("LinkedHashMap", "java.util.LinkedHashMap"), Map.entry("List", "java.util.List"), Map.entry("MutableList", "java.util.List"),
|
|
127
|
+
Map.entry("ArrayList", "java.util.ArrayList"), Map.entry("Set", "java.util.Set"), Map.entry("MutableSet", "java.util.Set"), Map.entry("HashSet", "java.util.HashSet"), Map.entry("Collection", "java.util.Collection"));
|
|
128
|
+
static final Map<String, String> UNBOX = Map.of("int", "java.lang.Integer", "long", "java.lang.Long", "short", "java.lang.Short", "byte", "java.lang.Byte", "char", "java.lang.Character", "boolean", "java.lang.Boolean", "float", "java.lang.Float", "double", "java.lang.Double");
|
|
129
|
+
// A name of a type as the file sees it: Kotlin's built-ins, the file's classes, its imports, the default imports.
|
|
130
|
+
String resolve(String n) {
|
|
131
|
+
if (BUILTIN.containsKey(n)) return BUILTIN.get(n);
|
|
132
|
+
String first = n.contains(".") ? n.substring(0, n.indexOf('.')) : n, rest = n.contains(".") ? n.substring(n.indexOf('.')) : "";
|
|
133
|
+
for (String k : classes.keySet()) { String d = k.replace('$', '.'); if (d.equals(n) || d.endsWith("." + n)) return k; }
|
|
134
|
+
if (imports.containsKey(first)) return bin(imports.get(first) + rest);
|
|
135
|
+
if (n.contains(".") && load(n) != null) return load(n).getName();
|
|
136
|
+
for (String s : stars) { Class<?> c = load(s + n); if (c != null) return c.getName(); }
|
|
137
|
+
return pkg.isEmpty() ? n : pkg + "." + n;
|
|
138
|
+
}
|
|
139
|
+
String bin(String q) { Class<?> c = load(q); return c != null ? c.getName() : q; }
|
|
140
|
+
static Class<?> load(String q) {
|
|
141
|
+
for (String t = q; ; ) {
|
|
142
|
+
try { return Class.forName(t, false, KtReader.class.getClassLoader()); } catch (ClassNotFoundException | NoClassDefFoundError e) { /* an outer class's nested one? */ }
|
|
143
|
+
int i = t.lastIndexOf('.');
|
|
144
|
+
if (i < 0) return null;
|
|
145
|
+
t = t.substring(0, i) + "$" + t.substring(i + 1);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
JT type(KtTypeReference tr) { return tr == null ? null : type(tr.getTypeElement()); }
|
|
149
|
+
JT type(KtTypeElement te) {
|
|
150
|
+
if (te instanceof KtNullableType nt) { JT in = type(nt.getInnerType()); if (in == null) return null; String b = UNBOX.get(in.n); return new JT(b != null ? b : in.n, in.a, true); }
|
|
151
|
+
if (te instanceof KtFunctionType ft) return new JT("kotlin.jvm.functions.Function" + ft.getParameters().size());
|
|
152
|
+
if (te instanceof KtUserType ut) {
|
|
153
|
+
List<String> parts = new ArrayList<>();
|
|
154
|
+
for (KtUserType u = ut; u != null; u = u.getQualifier()) parts.add(0, u.getReferencedName());
|
|
155
|
+
List<JT> as = new ArrayList<>();
|
|
156
|
+
for (KtTypeProjection p : ut.getTypeArguments()) { JT a = type(p.getTypeReference()); as.add(a == null ? new JT("java.lang.Object") : a.nul ? a : boxed(a)); }
|
|
157
|
+
return new JT(resolve(String.join(".", parts)), as, false);
|
|
158
|
+
}
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
static JT boxed(JT t) { String b = UNBOX.get(t.n); return b == null ? t : new JT(b, t.a, t.nul); }
|
|
162
|
+
// A JVM parameter's erased type, as the harness looks the method up.
|
|
163
|
+
static String erased(JT t) { if (t == null) return "java.lang.Object"; if (t.nul) return UNBOX.getOrDefault(t.n, t.n); return t.n.equals("void") ? "java.lang.Object" : t.n; }
|
|
164
|
+
@Override public List<String> enumValues(String fq) {
|
|
165
|
+
KtClassOrObject c = classes.get(fq);
|
|
166
|
+
if (c instanceof KtClass k && k.isEnum()) { List<String> out = new ArrayList<>(); for (KtDeclaration d : k.getBody().getDeclarations()) if (d instanceof KtEnumEntry e) out.add(e.getName()); return out; }
|
|
167
|
+
Class<?> j = c == null ? load(fq) : null;
|
|
168
|
+
if (j != null && j.isEnum()) { List<String> out = new ArrayList<>(); for (Object o : j.getEnumConstants()) out.add(((Enum<?>) o).name()); return out; }
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
@Override public String rec(String fq) {
|
|
172
|
+
KtClassOrObject c = classes.get(fq);
|
|
173
|
+
if (!(c instanceof KtClass k) || k.isEnum() || k.isInterface() || k.isSealed()) return null;
|
|
174
|
+
if (recs.containsKey(fq) || fq.equals(selfName)) return fq;
|
|
175
|
+
List<Object> fields = new ArrayList<>();
|
|
176
|
+
for (KtParameter p : k.getPrimaryConstructorParameters()) if (p.hasValOrVar()) fields.add(m("name", p.getName(), "t", stype(type(p.getTypeReference()), this)));
|
|
177
|
+
recs.put(fq, m("name", fq, "short", k.getName(), "fields", fields, "methods", m(), "kinds", k.isData() || fields.size() == k.getPrimaryConstructorParameters().size() ? List.of("dataclass") : List.of("value"), "own", true, "module", pkg, "p", where(k), "jvm", m("data", k.isData() ? true : null)));
|
|
178
|
+
return fq;
|
|
179
|
+
}
|
|
180
|
+
// @GdpSkip, @GdpAnswer("int"), @GdpStrict: the boundary's tags, as the executor reads a decorator.
|
|
181
|
+
List<Object> tagDecos(KtModifierListOwner o) {
|
|
182
|
+
List<Object> out = new ArrayList<>();
|
|
183
|
+
for (KtAnnotationEntry a : o.getAnnotationEntries()) if (a.getShortName() != null && gdpTag(a.getShortName().asString())) out.add(m("name", a.getShortName().asString(), "text", a.getText().replace("@", ""), "p", where(a)));
|
|
184
|
+
return out;
|
|
185
|
+
}
|
|
186
|
+
// Another module's class or an interface: a record of other code, its methods' declared results its `rets`.
|
|
187
|
+
boolean noOther = false;
|
|
188
|
+
@Override public String other(String fq) {
|
|
189
|
+
if (noOther || fq == null || fq.isEmpty() || recs.containsKey(fq) && !Boolean.FALSE.equals(((Map<?, ?>) recs.get(fq)).get("own")) && !recs.containsKey(fq)) return null;
|
|
190
|
+
if (recs.containsKey(fq)) return Boolean.FALSE.equals(((Map<?, ?>) recs.get(fq)).get("own")) ? fq : null;
|
|
191
|
+
KtClassOrObject k = classes.get(fq);
|
|
192
|
+
Map<String, Object> methods = new LinkedHashMap<>(), rets = new LinkedHashMap<>();
|
|
193
|
+
boolean proto;
|
|
194
|
+
noOther = true;
|
|
195
|
+
try {
|
|
196
|
+
if (k != null) {
|
|
197
|
+
if (!(k instanceof KtClass kc) || !kc.isInterface()) return null;
|
|
198
|
+
proto = true;
|
|
199
|
+
for (KtDeclaration d : body(k)) if (d instanceof KtNamedFunction f && f.getName() != null) { methods.put(f.getName(), null); JT t = type(f.getTypeReference()); rets.put(f.getName(), stype(t == null ? new JT("void") : t, this)); }
|
|
200
|
+
} else {
|
|
201
|
+
Class<?> c = load(fq);
|
|
202
|
+
if (c == null || c.isEnum() || c.isPrimitive() || c.isArray()) return null;
|
|
203
|
+
proto = c.isInterface();
|
|
204
|
+
Map<String, Integer> seen = new HashMap<>();
|
|
205
|
+
for (java.lang.reflect.Method mm : c.getMethods()) {
|
|
206
|
+
if (java.lang.reflect.Modifier.isStatic(mm.getModifiers()) || mm.getDeclaringClass() == Object.class) continue;
|
|
207
|
+
if (seen.merge(mm.getName(), 1, Integer::sum) > 1) { rets.remove(mm.getName()); continue; }
|
|
208
|
+
methods.put(mm.getName(), null);
|
|
209
|
+
rets.put(mm.getName(), stype(jtOf(mm.getReturnType(), mm.getGenericReturnType()), this));
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
} finally { noOther = false; }
|
|
213
|
+
recs.put(fq, m("name", fq, "short", fq.substring(Math.max(fq.lastIndexOf('.'), fq.lastIndexOf('$')) + 1), "fields", List.of(), "methods", methods, "kinds", proto ? List.of("protocol") : List.of(), "own", false, "module", fq.contains(".") ? fq.substring(0, fq.lastIndexOf('.')) : "", "p", "?", "rets", rets));
|
|
214
|
+
return fq;
|
|
215
|
+
}
|
|
216
|
+
List<Object> annos(KtModifierListOwner o) {
|
|
217
|
+
List<Object> out = new ArrayList<>();
|
|
218
|
+
for (KtAnnotationEntry a : o.getAnnotationEntries()) { String n = a.getShortName() == null ? a.getText() : a.getShortName().asString(); out.add(anno(n, resolve(n), a.getText())); }
|
|
219
|
+
return out;
|
|
220
|
+
}
|
|
221
|
+
// Injected by the framework: @Autowired, @Inject or @Resource on it (a constructor's argument: on the constructor too).
|
|
222
|
+
static boolean injected(KtModifierListOwner o) { for (KtAnnotationEntry a : o.getAnnotationEntries()) if (a.getShortName() != null && INJECT.contains(a.getShortName().asString())) return true; return false; }
|
|
223
|
+
static boolean has(KtModifierListOwner o, String n) { for (KtAnnotationEntry a : o.getAnnotationEntries()) if (a.getShortName() != null && a.getShortName().asString().equals(n)) return true; return false; }
|
|
224
|
+
|
|
225
|
+
// --- the class ---------------------------------------------------------------------------------------------------------
|
|
226
|
+
// Its supertypes of this file (a sealed or abstract parent's members), from the root down.
|
|
227
|
+
List<KtClassOrObject> chain(KtClassOrObject c) {
|
|
228
|
+
List<KtClassOrObject> out = new ArrayList<>();
|
|
229
|
+
for (KtClassOrObject k = c; k != null; ) {
|
|
230
|
+
out.add(0, k);
|
|
231
|
+
KtClassOrObject next = null;
|
|
232
|
+
for (KtSuperTypeListEntry e : k.getSuperTypeListEntries()) { JT t = type(e.getTypeReference()); if (t != null && classes.get(t.n) instanceof KtClass sc && !sc.isInterface()) next = sc; }
|
|
233
|
+
k = next;
|
|
234
|
+
}
|
|
235
|
+
return out;
|
|
236
|
+
}
|
|
237
|
+
boolean computed(KtProperty p) { return p.getInitializer() == null && p.getGetter() != null && p.getGetter().hasBody() && !p.getGetter().getText().contains("field"); }
|
|
238
|
+
String entry(String name) {
|
|
239
|
+
KtClassOrObject c = classes.get(name);
|
|
240
|
+
self = c; selfName = name;
|
|
241
|
+
Map<String, Object> methods = new LinkedHashMap<>(), sigs = new LinkedHashMap<>();
|
|
242
|
+
List<Object> fields = new ArrayList<>(), jfields = new ArrayList<>(), kinds = new ArrayList<>();
|
|
243
|
+
if (c instanceof KtClass k && !k.getTypeParameters().isEmpty()) kinds.add("generic:" + k.getTypeParameters().size());
|
|
244
|
+
if (c instanceof KtClass k && k.isInner()) return refused(c, name, "an inner class (it holds its outer object; the subset reads a top-level or nested class)");
|
|
245
|
+
List<KtClassOrObject> ch = chain(c);
|
|
246
|
+
boolean immutable = true;
|
|
247
|
+
List<String> myReducers = new ArrayList<>();
|
|
248
|
+
for (KtClassOrObject k : ch) {
|
|
249
|
+
if (k instanceof KtClass kc) for (KtParameter p : kc.getPrimaryConstructorParameters()) if (p.hasValOrVar()) {
|
|
250
|
+
immutable &= !p.isMutable();
|
|
251
|
+
JT t = type(p.getTypeReference());
|
|
252
|
+
fields.add(m("name", p.getName(), "t", stype(t, this), "inject", injected(p) ? true : null));
|
|
253
|
+
jfields.add(m("name", p.getName(), "jt", String.valueOf(t), "annos", annos(p), "tag", tag(p)));
|
|
254
|
+
}
|
|
255
|
+
for (KtDeclaration d : body(k)) if (d instanceof KtProperty p && !computed(p) && !p.hasModifier(KtTokens.CONST_KEYWORD)) {
|
|
256
|
+
immutable &= !p.isVar();
|
|
257
|
+
JT t = type(p.getTypeReference());
|
|
258
|
+
if (t == null && p.getInitializer() != null) t = ex(p.getInitializer(), new Ctx(new ArrayList<>(), new HashMap<>(), 0, true)).t();
|
|
259
|
+
fields.add(m("name", p.getName(), "t", stype(t, this), "inject", injected(p) ? true : null));
|
|
260
|
+
jfields.add(m("name", p.getName(), "jt", String.valueOf(t), "annos", annos(p), "tag", tag(p)));
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
// methods: the subclass's own first (an override hides its parent's)
|
|
264
|
+
Set<String> seen = new HashSet<>();
|
|
265
|
+
for (int i = ch.size() - 1; i >= 0; i--) {
|
|
266
|
+
KtClassOrObject k = ch.get(i);
|
|
267
|
+
for (KtDeclaration d : body(k)) {
|
|
268
|
+
if (d instanceof KtNamedFunction fn && fn.hasBody()) {
|
|
269
|
+
String key = key(k, fn);
|
|
270
|
+
if (!seen.add(key)) continue;
|
|
271
|
+
String id = binOf(k) + "#" + fn.getName() + "/" + fn.getValueParameters().size();
|
|
272
|
+
JT rt = type(fn.getTypeReference());
|
|
273
|
+
boolean reducer = immutable && rt != null && (rt.n.equals(name) || chain(c).stream().anyMatch(s -> binOf(s).equals(rt.n)));
|
|
274
|
+
methods.put(key, id);
|
|
275
|
+
funcs.put(id, func(fn, id, reducer, fields));
|
|
276
|
+
if (reducer) { reducers.add(key); myReducers.add(key); }
|
|
277
|
+
sigs.put(key, m("name", fn.getName(), "params", fn.getValueParameters().stream().map(p -> erased(type(p.getTypeReference()))).toList(), "decl", binOf(k)));
|
|
278
|
+
} else if (d instanceof KtProperty p && computed(p)) {
|
|
279
|
+
if (!seen.add(p.getName())) continue;
|
|
280
|
+
String id = binOf(k) + "#get:" + p.getName();
|
|
281
|
+
methods.put(p.getName(), id);
|
|
282
|
+
funcs.put(id, getter(p, id));
|
|
283
|
+
JT t = type(p.getTypeReference());
|
|
284
|
+
sigs.put(p.getName(), m("name", (p.getName().startsWith("is") && t != null && t.is("boolean") ? p.getName() : "get" + Character.toUpperCase(p.getName().charAt(0)) + p.getName().substring(1)), "params", List.of(), "decl", binOf(k)));
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
// the constructor: the primary one, the parents' property initializers before the class's own
|
|
289
|
+
methods.put("__init__", name + "#<init>");
|
|
290
|
+
funcs.put(name + "#<init>", ctor(c, ch));
|
|
291
|
+
List<Object> ps = c instanceof KtClass k ? k.getPrimaryConstructorParameters().stream().map(p -> (Object) erased(type(p.getTypeReference()))).toList() : List.of();
|
|
292
|
+
Map<String, Object> jctor = m("params", ps, "names", c instanceof KtClass k ? k.getPrimaryConstructorParameters().stream().map(KtParameter::getName).toList() : List.of(),
|
|
293
|
+
"jts", c instanceof KtClass k ? k.getPrimaryConstructorParameters().stream().map(p -> String.valueOf(type(p.getTypeReference()))).toList() : List.of(),
|
|
294
|
+
"paramAnnos", c instanceof KtClass k ? k.getPrimaryConstructorParameters().stream().map(this::annos).toList() : List.of(),
|
|
295
|
+
"annos", c instanceof KtClass k && k.getPrimaryConstructor() != null ? annos(k.getPrimaryConstructor()) : List.of());
|
|
296
|
+
recs.put(name, m("name", name, "short", c.getName(), "fields", fields, "methods", methods, "kinds", kinds, "own", true, "module", pkg, "p", where(c), "jvm", m("fields", jfields, "annos", annos(c), "ctor", jctor, "immutable", immutable ? true : null, "cls", name, "sigs", sigs, "reducers", new ArrayList<>(myReducers))));
|
|
297
|
+
jvm.put("cls", name); jvm.put("ctor", ps); jvm.put("methods", sigs);
|
|
298
|
+
return name;
|
|
299
|
+
}
|
|
300
|
+
static List<KtDeclaration> body(KtClassOrObject k) { return k.getBody() == null ? List.of() : k.getBody().getDeclarations(); }
|
|
301
|
+
String key(KtClassOrObject k, KtNamedFunction fn) {
|
|
302
|
+
long same = body(k).stream().filter(d -> d instanceof KtNamedFunction f && Objects.equals(f.getName(), fn.getName())).count();
|
|
303
|
+
String n = same > 1 ? fn.getName() + "/" + fn.getValueParameters().size() : fn.getName();
|
|
304
|
+
return fn.hasModifier(KtTokens.PRIVATE_KEYWORD) || Set.of("equals", "hashCode", "toString").contains(fn.getName()) ? "_" + n : n;
|
|
305
|
+
}
|
|
306
|
+
String refused(KtClassOrObject c, String name, String why) {
|
|
307
|
+
funcs.put(name + "#<init>", m("id", name + "#<init>", "name", "__init__", "p", where(c), "span", List.of(base, 0, 0), "params", List.of(m("name", "self", "t", m("k", "any"), "self", true)), "ret", m("k", "none"), "body", List.of(sno(where(c), why)), "decorators", List.of()));
|
|
308
|
+
recs.put(name, m("name", name, "short", c.getName(), "fields", List.of(), "methods", m("__init__", name + "#<init>"), "kinds", List.of(), "own", true, "module", pkg, "p", where(c)));
|
|
309
|
+
return name;
|
|
310
|
+
}
|
|
311
|
+
Map<String, Object> fn(String id, String name, PsiElement at, List<Object> params, JT ret, List<Object> body, KtModifierListOwner o) {
|
|
312
|
+
List<Object> ps = new ArrayList<>(List.of(m("name", "self", "t", m("k", "any"), "self", true)));
|
|
313
|
+
ps.addAll(params);
|
|
314
|
+
return m("id", id, "name", name, "cls", selfName, "p", where(at), "span", List.of(base, line(at.getTextRange().getStartOffset()), line(at.getTextRange().getEndOffset())), "params", ps, "ret", stype(ret, this), "body", body, "decorators", o == null ? List.of() : tagDecos(o),
|
|
315
|
+
"annos", o == null ? null : annos(o), "tag", tag(at), "jt", String.valueOf(ret));
|
|
316
|
+
}
|
|
317
|
+
Map<String, Object> func(KtNamedFunction f, String id, boolean reducer, List<Object> fields) {
|
|
318
|
+
Ctx c = new Ctx(new ArrayList<>(), new HashMap<>(), 0, false);
|
|
319
|
+
List<Object> params = new ArrayList<>();
|
|
320
|
+
for (KtParameter p : f.getValueParameters()) {
|
|
321
|
+
JT t = type(p.getTypeReference());
|
|
322
|
+
c.locals.put(p.getName(), t);
|
|
323
|
+
params.add(m("name", p.getName(), "t", pstype(t, this), "jt", String.valueOf(t), "annos", annos(p), "dflt", p.getDefaultValue() == null ? null : ex(p.getDefaultValue(), c.fresh(), t).ir()));
|
|
324
|
+
}
|
|
325
|
+
// (no declared type: Unit for a block body, the expression's for an expression body)
|
|
326
|
+
JT ret = f.getTypeReference() != null ? type(f.getTypeReference()) : f.hasBlockBody() ? new JT("void") : ex(f.getBodyExpression(), new Ctx(new ArrayList<>(), new HashMap<>(c.locals), 0, false)).t();
|
|
327
|
+
List<Object> body = new ArrayList<>();
|
|
328
|
+
String p = where(f);
|
|
329
|
+
if (f.getTypeParameters().size() > 0) body.add(sno(p, f.getName() + ": a generic function"));
|
|
330
|
+
else if (f.getReceiverTypeReference() != null) body.add(sno(p, f.getName() + ": an extension function"));
|
|
331
|
+
else if (f.hasBlockBody()) body.addAll(block(f.getBodyBlockExpression().getStatements(), c, ret));
|
|
332
|
+
else body.addAll(tail(f.getBodyExpression(), c, ret));
|
|
333
|
+
if (reducer) body = next(body, fields);
|
|
334
|
+
if (has(f, "Synchronized")) body = new ArrayList<>(List.of(with(body, p, "@Synchronized " + f.getName())));
|
|
335
|
+
Map<String, Object> out = fn(id, f.getName(), f, params, reducer ? null : ret, body, f);
|
|
336
|
+
if (reducer) out.put("ret", m("k", "none"));
|
|
337
|
+
if (f.hasModifier(KtTokens.SUSPEND_KEYWORD)) out.put("async", null);
|
|
338
|
+
return out;
|
|
339
|
+
}
|
|
340
|
+
// A method returning the object's next value (an immutable class's step): each return writes the fields from it.
|
|
341
|
+
List<Object> next(List<Object> body, List<Object> fields) {
|
|
342
|
+
List<Object> out = new ArrayList<>();
|
|
343
|
+
for (Object s0 : body) {
|
|
344
|
+
@SuppressWarnings("unchecked") Map<String, Object> s = new LinkedHashMap<>((Map<String, Object>) s0);
|
|
345
|
+
String k = (String) s.get("s"), p = (String) s.get("p");
|
|
346
|
+
if ("ret".equals(k) && s.get("x") != null) {
|
|
347
|
+
String v = "$next" + (tmp++);
|
|
348
|
+
out.add(s("set", p, "to", var(v, p), "x", s.get("x")));
|
|
349
|
+
for (Object f0 : fields) { String f = (String) ((Map<?, ?>) f0).get("name"); out.add(s("set", zero(), "to", get(self(p), f, p, "this." + f), "x", get(var(v, p), f, p, v + "." + f))); }
|
|
350
|
+
out.add(s("ret", p));
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
for (String b : List.of("then", "else", "body")) if (s.get(b) instanceof List<?> l) s.put(b, next(new ArrayList<>(l), fields));
|
|
354
|
+
if (s.get("cases") instanceof List<?> cs) { List<Object> nc = new ArrayList<>(); for (Object c0 : cs) { Map<String, Object> c = new LinkedHashMap<>((Map<String, Object>) c0); c.put("b", next(new ArrayList<>((List<?>) c.get("b")), fields)); nc.add(c); } s.put("cases", nc); }
|
|
355
|
+
out.add(s);
|
|
356
|
+
}
|
|
357
|
+
return out;
|
|
358
|
+
}
|
|
359
|
+
Map<String, Object> getter(KtProperty p, String id) {
|
|
360
|
+
Ctx c = new Ctx(new ArrayList<>(), new HashMap<>(), 0, false);
|
|
361
|
+
JT t = type(p.getTypeReference());
|
|
362
|
+
KtPropertyAccessor g = p.getGetter();
|
|
363
|
+
List<Object> body = g.hasBlockBody() ? block(((KtBlockExpression) g.getBodyExpression()).getStatements(), c, t) : tail(g.getBodyExpression(), c, t);
|
|
364
|
+
if (has(g, "Synchronized") || has(p, "Synchronized")) body = new ArrayList<>(List.of(with(body, where(p), "@Synchronized get")));
|
|
365
|
+
return fn(id, p.getName(), p, List.of(), t, body, p);
|
|
366
|
+
}
|
|
367
|
+
Map<String, Object> ctor(KtClassOrObject c, List<KtClassOrObject> ch) {
|
|
368
|
+
Ctx cx = new Ctx(new ArrayList<>(), new HashMap<>(), 0, true);
|
|
369
|
+
List<Object> params = new ArrayList<>(), body = new ArrayList<>();
|
|
370
|
+
String p = where(c), z = zero();
|
|
371
|
+
if (c instanceof KtClass k) for (KtParameter pa : k.getPrimaryConstructorParameters()) {
|
|
372
|
+
JT t = type(pa.getTypeReference());
|
|
373
|
+
cx.locals.put(pa.getName(), t);
|
|
374
|
+
boolean inj = injected(pa) || (k.getPrimaryConstructor() != null && injected(k.getPrimaryConstructor()) && iface(t == null ? "" : t.n));
|
|
375
|
+
params.add(m("name", pa.getName(), "t", pstype(t, this), "jt", String.valueOf(t), "inject", inj ? true : null, "annos", annos(pa), "dflt", pa.getDefaultValue() == null ? null : ex(pa.getDefaultValue(), cx.fresh(), t).ir()));
|
|
376
|
+
if (pa.hasValOrVar()) body.add(s("set", where(pa), "to", get(self(z), pa.getName(), z, "this." + pa.getName()), "x", var(pa.getName(), where(pa)), "t", stype(t, this)));
|
|
377
|
+
}
|
|
378
|
+
for (KtClassOrObject k : ch) for (KtDeclaration d : body(k)) {
|
|
379
|
+
if (d instanceof KtProperty pr && !computed(pr) && !pr.hasModifier(KtTokens.CONST_KEYWORD)) {
|
|
380
|
+
JT t = type(pr.getTypeReference());
|
|
381
|
+
if (pr.getInitializer() == null && injected(pr)) continue;
|
|
382
|
+
if (pr.getInitializer() == null) { body.add(s("set", z, "to", get(self(z), pr.getName(), z, "this." + pr.getName()), "x", x("none", z), "t", stype(t, this))); continue; }
|
|
383
|
+
Ctx sc = cx.fresh();
|
|
384
|
+
X v = ex(pr.getInitializer(), sc, t);
|
|
385
|
+
if (t == null) t = v.t();
|
|
386
|
+
body.addAll(sc.pre);
|
|
387
|
+
body.add(s("set", where(pr), "to", get(self(z), pr.getName(), z, "this." + pr.getName()), "x", at(v.ir(), v.t(), t), "t", stype(t, this)));
|
|
388
|
+
} else if (d instanceof KtAnonymousInitializer ai && ai.getBody() instanceof KtBlockExpression b) body.addAll(block(b.getStatements(), cx, null));
|
|
389
|
+
}
|
|
390
|
+
return m("id", binOf(c) + "#<init>", "name", "__init__", "cls", selfName, "p", p, "span", List.of(base, 0, 0), "params", prepend(m("name", "self", "t", m("k", "any"), "self", true), params), "ret", m("k", "none"), "body", body, "decorators", List.of());
|
|
391
|
+
}
|
|
392
|
+
static List<Object> prepend(Object a, List<Object> xs) { List<Object> out = new ArrayList<>(); out.add(a); out.addAll(xs); return out; }
|
|
393
|
+
|
|
394
|
+
// --- statements ------------------------------------------------------------------------------------------------------
|
|
395
|
+
List<Object> block(List<KtExpression> ss, Ctx c0, JT ret) {
|
|
396
|
+
List<Object> out = new ArrayList<>();
|
|
397
|
+
Ctx c = c0.scope();
|
|
398
|
+
for (KtExpression s : ss) out.addAll(stmt(s, c, ret));
|
|
399
|
+
return out;
|
|
400
|
+
}
|
|
401
|
+
// A statement, with what its expression needed before it.
|
|
402
|
+
List<Object> stmt(KtExpression e, Ctx c0, JT ret) {
|
|
403
|
+
Ctx c = new Ctx(new ArrayList<>(), c0.locals, 0, c0.init);
|
|
404
|
+
List<Object> s = stmt1(e, c, ret);
|
|
405
|
+
List<Object> out = new ArrayList<>(c.pre);
|
|
406
|
+
out.addAll(s);
|
|
407
|
+
return out;
|
|
408
|
+
}
|
|
409
|
+
List<Object> stmt1(KtExpression e, Ctx c, JT ret) {
|
|
410
|
+
String p = where(e);
|
|
411
|
+
if (e instanceof KtProperty v) {
|
|
412
|
+
JT t = type(v.getTypeReference());
|
|
413
|
+
X x = v.getInitializer() == null ? new X(x("none", p), t) : ex(v.getInitializer(), c, t);
|
|
414
|
+
if (t == null) t = x.t();
|
|
415
|
+
c.locals.put(v.getName(), t);
|
|
416
|
+
return List.of(s("set", p, "to", var(v.getName(), p), "x", at(x.ir(), x.t(), t), "t", stype(t, this)));
|
|
417
|
+
}
|
|
418
|
+
if (e instanceof KtReturnExpression r) return r.getReturnedExpression() == null ? List.of(s("ret", p)) : tail(r.getReturnedExpression(), c, ret);
|
|
419
|
+
if (e instanceof KtThrowExpression) return List.of(fail(p, text(e)));
|
|
420
|
+
if (e instanceof KtIfExpression i) {
|
|
421
|
+
X cond = ex(i.getCondition(), c);
|
|
422
|
+
return List.of(s("if", p, "c", cond.ir(), "then", branch(i.getThen(), c, ret), "else", i.getElse() == null ? List.of() : branch(i.getElse(), c, ret)));
|
|
423
|
+
}
|
|
424
|
+
if (e instanceof KtWhenExpression w) return when(w, c, ret, false);
|
|
425
|
+
if (e instanceof KtBlockExpression b) return block(b.getStatements(), c, ret);
|
|
426
|
+
if (e instanceof KtWhileExpression w) return once(w.getCondition(), w.getBody(), c, ret, p);
|
|
427
|
+
if (e instanceof KtDoWhileExpression w) return once(w.getCondition(), w.getBody(), c, ret, p);
|
|
428
|
+
if (e instanceof KtForExpression) return List.of(sno(p, "a loop over a collection"));
|
|
429
|
+
if (e instanceof KtBreakExpression b) return b.getLabelName() == null ? List.of(s("break", p)) : List.of(sno(p, "a labeled break"));
|
|
430
|
+
if (e instanceof KtContinueExpression) return List.of(sno(p, "continue (a loop's next pass: the subset reads a compare-and-set loop's first)"));
|
|
431
|
+
if (e instanceof KtTryExpression) return List.of(sno(p, "try (an exception caught: the subset reads an exception only as the step's failure)"));
|
|
432
|
+
if (e instanceof KtBinaryExpression b) {
|
|
433
|
+
IElementType op = b.getOperationToken();
|
|
434
|
+
if (op == KtTokens.EQ) return assign(b.getLeft(), b.getRight(), c, p);
|
|
435
|
+
String aop = op == KtTokens.PLUSEQ ? "+" : op == KtTokens.MINUSEQ ? "-" : op == KtTokens.MULTEQ ? "*" : op == KtTokens.DIVEQ ? "/" : op == KtTokens.PERCEQ ? "%" : null;
|
|
436
|
+
if (aop != null) {
|
|
437
|
+
X l = ex(b.getLeft(), c), r = ex(b.getRight(), c);
|
|
438
|
+
Map<String, Object> to = place(b.getLeft(), c);
|
|
439
|
+
if (to == null) return List.of(sno(p, text(b.getLeft()) + ": an assignment the subset doesn't read"));
|
|
440
|
+
if (l.t() != null && l.t().is("java.lang.String")) return List.of(s("set", p, "to", to, "x", x("bin", p, "op", "+", "x", l.ir(), "y", r.ir(), "text", text(e))));
|
|
441
|
+
if (l.t() != null && map(l.t())) return List.of(sno(p, text(e) + ": a map's plus (the subset writes one key a step)"));
|
|
442
|
+
return List.of(s("set", p, "to", to, "x", arith(aop, l.ir(), l.t(), r.ir(), r.t(), l.t(), "wrap", p, text(e))));
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
if (e instanceof KtUnaryExpression u && (u.getOperationToken() == KtTokens.PLUSPLUS || u.getOperationToken() == KtTokens.MINUSMINUS) && !(e instanceof KtPrefixExpression pe && pe.getOperationToken() == KtTokens.EXCL)) {
|
|
446
|
+
X v = ex(u.getBaseExpression(), c);
|
|
447
|
+
Map<String, Object> to = place(u.getBaseExpression(), c);
|
|
448
|
+
if (to == null) return List.of(sno(p, text(e) + ": an assignment the subset doesn't read"));
|
|
449
|
+
return List.of(s("set", p, "to", to, "x", arith(u.getOperationToken() == KtTokens.PLUSPLUS ? "+" : "-", v.ir(), v.t(), lit(1, v.t(), p), v.t(), v.t(), "wrap", p, text(e))));
|
|
450
|
+
}
|
|
451
|
+
// a call as a statement (a map's put, an atomic's set, a check, synchronized and withLock, a scope function)
|
|
452
|
+
X x = exStmt(e, c, ret);
|
|
453
|
+
if (x == null) return List.of();
|
|
454
|
+
if ("$stmts".equals(x.ir().get("e"))) { @SuppressWarnings("unchecked") List<Object> ss = (List<Object>) x.ir().get("ss"); return ss; }
|
|
455
|
+
return List.of(s("expr", p, "x", x.ir()));
|
|
456
|
+
}
|
|
457
|
+
List<Object> branch(KtExpression e, Ctx c, JT ret) { Ctx b = c.scope(); return e instanceof KtBlockExpression bl ? block(bl.getStatements(), b, ret) : stmt(e, b, ret); }
|
|
458
|
+
// A return of an if or a when: the return pushed into its branches (each its own value, as statements).
|
|
459
|
+
List<Object> tail(KtExpression e, Ctx c, JT ret) {
|
|
460
|
+
String p = where(e);
|
|
461
|
+
if (e instanceof KtParenthesizedExpression pe) return tail(pe.getExpression(), c, ret);
|
|
462
|
+
if (e instanceof KtIfExpression i && i.getElse() != null) {
|
|
463
|
+
X cond = ex(i.getCondition(), c);
|
|
464
|
+
return List.of(s("if", p, "c", cond.ir(), "then", tailBranch(i.getThen(), c, ret), "else", tailBranch(i.getElse(), c, ret)));
|
|
465
|
+
}
|
|
466
|
+
if (e instanceof KtWhenExpression w) return when(w, c, ret, true);
|
|
467
|
+
if (e instanceof KtBlockExpression b) {
|
|
468
|
+
List<KtExpression> ss = b.getStatements();
|
|
469
|
+
Ctx sc = c.scope();
|
|
470
|
+
List<Object> out = new ArrayList<>();
|
|
471
|
+
for (int i = 0; i < ss.size() - 1; i++) out.addAll(stmt(ss.get(i), sc, ret));
|
|
472
|
+
if (!ss.isEmpty()) out.addAll(tail(ss.get(ss.size() - 1), sc, ret));
|
|
473
|
+
return out;
|
|
474
|
+
}
|
|
475
|
+
if (e instanceof KtThrowExpression || e instanceof KtReturnExpression) return stmt1(e, c, ret);
|
|
476
|
+
X x = ex(e, c, ret);
|
|
477
|
+
return List.of(s("ret", p, "x", at(x.ir(), x.t(), ret)));
|
|
478
|
+
}
|
|
479
|
+
List<Object> tailBranch(KtExpression e, Ctx c, JT ret) { Ctx b = new Ctx(new ArrayList<>(), new HashMap<>(c.locals), 0, c.init); List<Object> t = tail(e, b, ret); List<Object> out = new ArrayList<>(b.pre); out.addAll(t); return out; }
|
|
480
|
+
// when: a chain of ifs on == (a subject's value bound once), `is` a type test the reader decides where it can, `in`
|
|
481
|
+
// a range's bounds.
|
|
482
|
+
List<Object> when(KtWhenExpression w, Ctx c, JT ret, boolean asTail) {
|
|
483
|
+
String p = where(w);
|
|
484
|
+
List<Object> out = new ArrayList<>();
|
|
485
|
+
X subj = null;
|
|
486
|
+
String sv = null;
|
|
487
|
+
if (w.getSubjectExpression() != null) {
|
|
488
|
+
subj = ex(w.getSubjectExpression(), c);
|
|
489
|
+
sv = "$when" + (tmp++);
|
|
490
|
+
if (w.getSubjectVariable() != null) c.locals.put(w.getSubjectVariable().getName(), subj.t());
|
|
491
|
+
out.add(s("set", p, "to", var(w.getSubjectVariable() != null ? w.getSubjectVariable().getName() : sv, p), "x", subj.ir()));
|
|
492
|
+
if (w.getSubjectVariable() != null) sv = w.getSubjectVariable().getName();
|
|
493
|
+
}
|
|
494
|
+
List<Object> acc = List.of();
|
|
495
|
+
List<KtWhenEntry> es = w.getEntries();
|
|
496
|
+
boolean exhaustive = es.stream().anyMatch(KtWhenEntry::isElse);
|
|
497
|
+
if (asTail && !exhaustive) acc = List.of(sno(p, "a when with no else as a value"));
|
|
498
|
+
for (int i = es.size() - 1; i >= 0; i--) {
|
|
499
|
+
KtWhenEntry en = es.get(i);
|
|
500
|
+
List<Object> arm = asTail ? tailBranch(en.getExpression(), c, ret) : branch(en.getExpression(), c, ret);
|
|
501
|
+
if (en.isElse()) { acc = arm; continue; }
|
|
502
|
+
List<Object> conds = new ArrayList<>();
|
|
503
|
+
for (KtWhenCondition wc : en.getConditions()) conds.add(cond(wc, sv, subj == null ? null : subj.t(), c, where(wc)));
|
|
504
|
+
acc = List.of(s("if", zero(), "c", conds.size() == 1 ? conds.get(0) : x("or", where(en), "xs", conds), "then", arm, "else", acc));
|
|
505
|
+
}
|
|
506
|
+
out.addAll(acc);
|
|
507
|
+
return out;
|
|
508
|
+
}
|
|
509
|
+
Object cond(KtWhenCondition wc, String sv, JT st, Ctx c, String p) {
|
|
510
|
+
if (wc instanceof KtWhenConditionWithExpression we) {
|
|
511
|
+
if (sv == null) return ex(we.getExpression(), c).ir();
|
|
512
|
+
X v = ex(we.getExpression(), c, st);
|
|
513
|
+
return cmp("==", var(sv, p), v.ir(), p, text(wc));
|
|
514
|
+
}
|
|
515
|
+
if (wc instanceof KtWhenConditionIsPattern ip) {
|
|
516
|
+
JT t = type(ip.getTypeReference());
|
|
517
|
+
Boolean is = isType(sv == null ? null : st, t);
|
|
518
|
+
if (is == null) return no(p, text(wc) + ": a type test the reader can't decide");
|
|
519
|
+
return x("bool", p, "v", ip.isNegated() != is);
|
|
520
|
+
}
|
|
521
|
+
if (wc instanceof KtWhenConditionInRange ir && ir.getRangeExpression() instanceof KtBinaryExpression r && r.getOperationToken() == KtTokens.RANGE && sv != null) {
|
|
522
|
+
X lo = ex(r.getLeft(), c), hi = ex(r.getRight(), c);
|
|
523
|
+
Object in = x("and", p, "xs", List.of(cmp("<=", lo.ir(), var(sv, p), p, text(wc)), cmp("<=", var(sv, p), hi.ir(), p, text(wc))));
|
|
524
|
+
return ir.isNegated() ? not(in, p) : in;
|
|
525
|
+
}
|
|
526
|
+
return no(p, text(wc) + ": a when condition outside the subset");
|
|
527
|
+
}
|
|
528
|
+
// Whether a value of type a is a t: decided where a is the class read or one of its parents.
|
|
529
|
+
Boolean isType(JT a, JT t) {
|
|
530
|
+
if (a == null || t == null) return null;
|
|
531
|
+
if (a.n.equals(t.n)) return true;
|
|
532
|
+
KtClassOrObject ac = classes.get(a.n);
|
|
533
|
+
if (ac == null) return null;
|
|
534
|
+
if (chain(ac).stream().anyMatch(k -> binOf(k).equals(t.n))) return true;
|
|
535
|
+
if (classes.containsKey(t.n) && a.n.equals(selfName)) return false;
|
|
536
|
+
return null;
|
|
537
|
+
}
|
|
538
|
+
// while (true) / do ... while (true): a compare-and-set retry loop, read as its first pass.
|
|
539
|
+
List<Object> once(KtExpression cond, KtExpression body, Ctx c, JT ret, String p) {
|
|
540
|
+
if (!(cond instanceof KtConstantExpression k && k.getText().equals("true"))) return List.of(sno(p, "a loop"));
|
|
541
|
+
List<Object> b = new ArrayList<>(branch(body, c, ret));
|
|
542
|
+
b.add(sno(p, "the loop goes around again (the subset reads a retry loop's first pass: a compare-and-set that succeeds when calls don't interleave)"));
|
|
543
|
+
return List.of(breakable(b, p));
|
|
544
|
+
}
|
|
545
|
+
List<Object> assign(KtExpression l, KtExpression r, Ctx c, String p) {
|
|
546
|
+
if (l instanceof KtArrayAccessExpression aa && aa.getIndexExpressions().size() == 1) {
|
|
547
|
+
X m = ex(aa.getArrayExpression(), c), k = ex(aa.getIndexExpressions().get(0), c), v = ex(r, c, m.t() == null ? null : m.t().arg(1));
|
|
548
|
+
if (m.t() == null || !map(m.t())) return List.of(sno(p, text(l) + ": an item of other than a map"));
|
|
549
|
+
return List.of(s("set", p, "to", x("idx", p, "x", m.ir(), "i", at(k.ir(), k.t(), m.t().arg(0)), "text", text(l)), "x", v.ir()));
|
|
550
|
+
}
|
|
551
|
+
Map<String, Object> to = place(l, c);
|
|
552
|
+
if (to == null) return List.of(sno(p, text(l) + ": an assignment the subset doesn't read"));
|
|
553
|
+
X tl = ex(l, c.fresh()), v = ex(r, c, tl.t());
|
|
554
|
+
return List.of(s("set", p, "to", to, "x", at(v.ir(), v.t(), tl.t())));
|
|
555
|
+
}
|
|
556
|
+
Map<String, Object> place(KtExpression l, Ctx c) {
|
|
557
|
+
String p = where(l);
|
|
558
|
+
if (l instanceof KtNameReferenceExpression n) {
|
|
559
|
+
String id = n.getReferencedName();
|
|
560
|
+
if (c.locals.containsKey(id)) return var(id, p);
|
|
561
|
+
if (field(id) != null) return get(self(p), id, p, id);
|
|
562
|
+
return null;
|
|
563
|
+
}
|
|
564
|
+
if (l instanceof KtDotQualifiedExpression d && d.getSelectorExpression() instanceof KtNameReferenceExpression n) {
|
|
565
|
+
X o = ex(d.getReceiverExpression(), c);
|
|
566
|
+
return get(o.ir(), n.getReferencedName(), p, text(l));
|
|
567
|
+
}
|
|
568
|
+
return null;
|
|
569
|
+
}
|
|
570
|
+
// A field of the class read (a property with a backing field, a val/var constructor parameter), or its type.
|
|
571
|
+
JT field(String name) {
|
|
572
|
+
for (KtClassOrObject k : chain(self)) {
|
|
573
|
+
if (k instanceof KtClass kc) for (KtParameter p : kc.getPrimaryConstructorParameters()) if (p.hasValOrVar() && name.equals(p.getName())) return orObject(type(p.getTypeReference()));
|
|
574
|
+
for (KtDeclaration d : body(k)) if (d instanceof KtProperty pr && name.equals(pr.getName()) && !computed(pr) && !pr.hasModifier(KtTokens.CONST_KEYWORD)) {
|
|
575
|
+
JT t = type(pr.getTypeReference());
|
|
576
|
+
return t != null ? t : pr.getInitializer() != null ? orObject(ex(pr.getInitializer(), new Ctx(new ArrayList<>(), new HashMap<>(), 0, true)).t()) : new JT("java.lang.Object");
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
return null;
|
|
580
|
+
}
|
|
581
|
+
static JT orObject(JT t) { return t == null ? new JT("java.lang.Object") : t; }
|
|
582
|
+
KtProperty computedProp(String name) { for (KtClassOrObject k : chain(self)) for (KtDeclaration d : body(k)) if (d instanceof KtProperty pr && name.equals(pr.getName()) && computed(pr)) return pr; return null; }
|
|
583
|
+
KtProperty constant(String name) {
|
|
584
|
+
for (KtDeclaration d : file.getDeclarations()) if (d instanceof KtProperty pr && name.equals(pr.getName()) && pr.hasModifier(KtTokens.CONST_KEYWORD)) return pr;
|
|
585
|
+
for (KtClassOrObject k : chain(self)) for (KtObjectDeclaration o : k instanceof KtClass kc ? kc.getCompanionObjects() : List.<KtObjectDeclaration>of()) for (KtDeclaration d : body(o)) if (d instanceof KtProperty pr && name.equals(pr.getName()) && (pr.hasModifier(KtTokens.CONST_KEYWORD) || !pr.isVar())) return pr;
|
|
586
|
+
for (KtClassOrObject k : chain(self)) for (KtDeclaration d : body(k)) if (d instanceof KtProperty pr && name.equals(pr.getName()) && pr.hasModifier(KtTokens.CONST_KEYWORD)) return pr;
|
|
587
|
+
return null;
|
|
588
|
+
}
|
|
589
|
+
KtNamedFunction method(String name, int arity) {
|
|
590
|
+
List<KtClassOrObject> ch = chain(self);
|
|
591
|
+
for (int i = ch.size() - 1; i >= 0; i--) for (KtDeclaration d : body(ch.get(i))) if (d instanceof KtNamedFunction f && name.equals(f.getName()) && f.getValueParameters().size() >= arity) return f;
|
|
592
|
+
return null;
|
|
593
|
+
}
|
|
594
|
+
KtClassOrObject ownerOf(KtNamedFunction f) { for (KtClassOrObject k : chain(self)) if (body(k).contains(f)) return k; return self; }
|
|
595
|
+
|
|
596
|
+
// --- expressions -----------------------------------------------------------------------------------------------------
|
|
597
|
+
X ex(KtExpression e, Ctx c) { return ex(e, c, null); }
|
|
598
|
+
X no1(PsiElement e, String why) { return new X(no(where(e), text(e) + ": " + why), null); }
|
|
599
|
+
// An expression where its value is wanted (`want`: the type it is given to, which an integer literal takes).
|
|
600
|
+
X ex(KtExpression e, Ctx c, JT want) {
|
|
601
|
+
String p = where(e), t = text(e);
|
|
602
|
+
if (e instanceof KtParenthesizedExpression pe) return ex(pe.getExpression(), c, want);
|
|
603
|
+
if (e instanceof KtAnnotatedExpression ae) return ex(ae.getBaseExpression(), c, want);
|
|
604
|
+
if (e instanceof KtLabeledExpression le) return ex(le.getBaseExpression(), c, want);
|
|
605
|
+
if (e instanceof KtConstantExpression k) {
|
|
606
|
+
IElementType et = k.getNode().getElementType();
|
|
607
|
+
String s = k.getText().replace("_", "");
|
|
608
|
+
if (et == KtNodeTypes.BOOLEAN_CONSTANT) return new X(x("bool", p, "v", s.equals("true")), new JT("boolean"));
|
|
609
|
+
if (et == KtNodeTypes.NULL) return new X(x("none", p), new JT("null"));
|
|
610
|
+
if (et == KtNodeTypes.INTEGER_CONSTANT) {
|
|
611
|
+
boolean lng = s.endsWith("L") || s.endsWith("l");
|
|
612
|
+
String d = s.replaceAll("[lLuU]+$", "");
|
|
613
|
+
long v = d.startsWith("0x") || d.startsWith("0X") ? Long.parseLong(d.substring(2), 16) : d.startsWith("0b") || d.startsWith("0B") ? Long.parseLong(d.substring(2), 2) : Long.parseLong(d);
|
|
614
|
+
JT ty = lng || v > Integer.MAX_VALUE || v < Integer.MIN_VALUE ? new JT("long") : want != null && integral(want) ? new JT(prim(want)) : new JT("int");
|
|
615
|
+
return new X(lit(v, ty, p), ty);
|
|
616
|
+
}
|
|
617
|
+
if (et == KtNodeTypes.FLOAT_CONSTANT) { double d = Double.parseDouble(s.replaceAll("[fF]$", "")); return d == Math.floor(d) && Math.abs(d) < 9.007199254740992E15 ? new X(x("int", p, "v", Long.toString((long) d), "float", true), new JT(s.endsWith("f") || s.endsWith("F") ? "float" : "double")) : no1(e, "a float that isn't whole (a binary fraction)"); }
|
|
618
|
+
if (et == KtNodeTypes.CHARACTER_CONSTANT) return no1(e, "a character literal");
|
|
619
|
+
}
|
|
620
|
+
if (e instanceof KtStringTemplateExpression st) {
|
|
621
|
+
if (!st.hasInterpolation()) { StringBuilder b = new StringBuilder(); for (KtStringTemplateEntry en : st.getEntries()) b.append(en instanceof KtEscapeStringTemplateEntry es ? es.getUnescapedValue() : en.getText()); return new X(str(b.toString(), p), new JT("java.lang.String")); }
|
|
622
|
+
List<Object> parts = new ArrayList<>();
|
|
623
|
+
for (KtStringTemplateEntry en : st.getEntries()) parts.add(en.getExpression() == null ? en.getText() : m("x", ex(en.getExpression(), c.under()).ir(), "plain", true));
|
|
624
|
+
return new X(x("fstr", p, "parts", parts, "text", t), new JT("java.lang.String"));
|
|
625
|
+
}
|
|
626
|
+
if (e instanceof KtThisExpression) return new X(self(p), new JT(selfName));
|
|
627
|
+
if (e instanceof KtNameReferenceExpression n) return name(n, c, p, t);
|
|
628
|
+
if (e instanceof KtDotQualifiedExpression d) return dot(d, c, p, t, want, false);
|
|
629
|
+
if (e instanceof KtSafeQualifiedExpression sq) return safe(sq, c, p, t);
|
|
630
|
+
if (e instanceof KtCallExpression ce) return call(ce, null, null, c, p, t, want, false);
|
|
631
|
+
if (e instanceof KtArrayAccessExpression aa && aa.getIndexExpressions().size() == 1) {
|
|
632
|
+
X m = ex(aa.getArrayExpression(), c), k = ex(aa.getIndexExpressions().get(0), c);
|
|
633
|
+
if (m.t() == null || !map(m.t())) return no1(e, "an item of other than a map");
|
|
634
|
+
return new X(Jvm.call(get(m.ir(), "get", p, t), List.of(at(k.ir(), k.t(), m.t().arg(0))), p, t), m.t().arg(1).nullable(true));
|
|
635
|
+
}
|
|
636
|
+
if (e instanceof KtIfExpression i) {
|
|
637
|
+
if (i.getElse() == null) return no1(e, "an if with no else as a value");
|
|
638
|
+
X cond = ex(i.getCondition(), c);
|
|
639
|
+
X a = value(i.getThen(), c.under(), want), b = value(i.getElse(), c.under(), want);
|
|
640
|
+
JT ty = a.t() == null || a.t().is("null") ? (b.t() == null ? null : b.t().nullable(a.t() != null)) : b.t() != null && b.t().is("null") ? a.t().nullable(true) : a.t();
|
|
641
|
+
if (num(a.t()) && num(b.t())) ty = promote(a.t(), b.t());
|
|
642
|
+
return new X(x("ite", p, "c", cond.ir(), "a", at(a.ir(), a.t(), ty), "b", at(b.ir(), b.t(), ty)), ty);
|
|
643
|
+
}
|
|
644
|
+
if (e instanceof KtWhenExpression w) return no1(e, "a when as a value inside an expression (the subset reads one as a statement or a return)");
|
|
645
|
+
if (e instanceof KtPrefixExpression pe) {
|
|
646
|
+
IElementType op = pe.getOperationToken();
|
|
647
|
+
X v = ex(pe.getBaseExpression(), c, want);
|
|
648
|
+
if (op == KtTokens.EXCL) return new X(not(v.ir(), p), new JT("boolean"));
|
|
649
|
+
if (op == KtTokens.MINUS) return floating(v.t()) ? new X(x("neg", p, "x", v.ir()), v.t()) : new X(x("unop", p, "op", "-", "x", at(v.ir(), v.t(), v.t()), "text", t), v.t());
|
|
650
|
+
if (op == KtTokens.PLUS) return v;
|
|
651
|
+
return no1(e, "an assignment inside an expression");
|
|
652
|
+
}
|
|
653
|
+
if (e instanceof KtPostfixExpression pe) {
|
|
654
|
+
if (pe.getOperationToken() == KtTokens.EXCLEXCL) { X v = ex(pe.getBaseExpression(), c); return new X(x("expect", p, "x", v.ir(), "text", t + " (a NullPointerException)"), v.t() == null ? null : v.t().nullable(false)); }
|
|
655
|
+
return no1(e, "an assignment inside an expression");
|
|
656
|
+
}
|
|
657
|
+
if (e instanceof KtBinaryExpressionWithTypeRHS as) {
|
|
658
|
+
X v = ex(as.getLeft(), c);
|
|
659
|
+
JT to = type(as.getRight());
|
|
660
|
+
Boolean is = isType(v.t(), to);
|
|
661
|
+
return Boolean.TRUE.equals(is) ? new X(v.ir(), to) : no1(e, "a cast the reader can't decide");
|
|
662
|
+
}
|
|
663
|
+
if (e instanceof KtIsExpression ie) {
|
|
664
|
+
X v = ex(ie.getLeftHandSide(), c);
|
|
665
|
+
Boolean is = isType(v.t(), type(ie.getTypeReference()));
|
|
666
|
+
return is == null ? no1(e, "a type test the reader can't decide") : new X(x("bool", p, "v", ie.isNegated() != is), new JT("boolean"));
|
|
667
|
+
}
|
|
668
|
+
if (e instanceof KtBinaryExpression b) return binary(b, c, p, t, want);
|
|
669
|
+
if (e instanceof KtLambdaExpression || e instanceof KtCallableReferenceExpression) return new X(x("opaque", p, "why", t + ": a lambda (a closure)"), new JT("kotlin.jvm.functions.Function0"));
|
|
670
|
+
if (e instanceof KtObjectLiteralExpression) return no1(e, "an object expression");
|
|
671
|
+
if (e instanceof KtThrowExpression || e instanceof KtReturnExpression || e instanceof KtBreakExpression || e instanceof KtContinueExpression) return no1(e, "a jump inside an expression");
|
|
672
|
+
return no1(e, e.getClass().getSimpleName().replace("Kt", "") + " (outside the subset)");
|
|
673
|
+
}
|
|
674
|
+
// A branch's value: a block's last expression after its statements (which go before it).
|
|
675
|
+
X value(KtExpression e, Ctx c, JT want) {
|
|
676
|
+
if (e instanceof KtBlockExpression b) {
|
|
677
|
+
List<KtExpression> ss = b.getStatements();
|
|
678
|
+
if (ss.isEmpty()) return new X(x("none", where(e)), new JT("void"));
|
|
679
|
+
if (ss.size() > 1) return no1(e, "a block of several statements as a value");
|
|
680
|
+
return ex(ss.get(0), c, want);
|
|
681
|
+
}
|
|
682
|
+
return ex(e, c, want);
|
|
683
|
+
}
|
|
684
|
+
X binary(KtBinaryExpression b, Ctx c, String p, String t, JT want) {
|
|
685
|
+
IElementType op = b.getOperationToken();
|
|
686
|
+
if (op == KtTokens.ANDAND || op == KtTokens.OROR) { X l = ex(b.getLeft(), c), r = ex(b.getRight(), c.under()); return new X(x(op == KtTokens.ANDAND ? "and" : "or", p, "xs", List.of(l.ir(), r.ir())), new JT("boolean")); }
|
|
687
|
+
if (op == KtTokens.ELVIS) {
|
|
688
|
+
X l = ex(b.getLeft(), c);
|
|
689
|
+
KtExpression r = b.getRight();
|
|
690
|
+
if (r instanceof KtReturnExpression || r instanceof KtThrowExpression) {
|
|
691
|
+
if (c.cond > 0) return no1(b, "?: return inside a condition");
|
|
692
|
+
String v = "$elvis" + (tmp++);
|
|
693
|
+
c.pre.add(s("set", p, "to", var(v, p), "x", l.ir()));
|
|
694
|
+
c.locals.put(v, l.t());
|
|
695
|
+
c.pre.add(s("if", p, "c", cmp("is", var(v, p), x("none", p), p, t), "then", stmt(r, c, null), "else", List.of()));
|
|
696
|
+
return new X(var(v, p), l.t() == null ? null : l.t().nullable(false));
|
|
697
|
+
}
|
|
698
|
+
X rv = ex(r, c.under(), l.t() == null ? want : l.t().nullable(false));
|
|
699
|
+
JT ty = l.t() == null ? rv.t() : l.t().nullable(rv.t() != null && rv.t().nul);
|
|
700
|
+
return new X(x("opt_or", p, "x", l.ir(), "d", at(rv.ir(), rv.t(), ty == null ? null : ty.nullable(false))), ty);
|
|
701
|
+
}
|
|
702
|
+
if (op == KtTokens.IN_KEYWORD || op == KtTokens.NOT_IN) {
|
|
703
|
+
X k = ex(b.getLeft(), c), m = ex(b.getRight(), c);
|
|
704
|
+
if (b.getRight() instanceof KtBinaryExpression r && r.getOperationToken() == KtTokens.RANGE) {
|
|
705
|
+
X lo = ex(r.getLeft(), c), hi = ex(r.getRight(), c);
|
|
706
|
+
Object in = x("and", p, "xs", List.of(cmp("<=", lo.ir(), k.ir(), p, t), cmp("<=", k.ir(), hi.ir(), p, t)));
|
|
707
|
+
return new X(op == KtTokens.IN_KEYWORD ? (Map<String, Object>) in : not(in, p), new JT("boolean"));
|
|
708
|
+
}
|
|
709
|
+
if (m.t() == null || !map(m.t())) return no1(b, "in of other than a map or a range");
|
|
710
|
+
return new X(cmp(op == KtTokens.IN_KEYWORD ? "in" : "not in", at(k.ir(), k.t(), m.t().arg(0)), m.ir(), p, t), new JT("boolean"));
|
|
711
|
+
}
|
|
712
|
+
X l = ex(b.getLeft(), c), r = ex(b.getRight(), c, l.t());
|
|
713
|
+
if (op == KtTokens.EQEQ || op == KtTokens.EXCLEQ || op == KtTokens.EQEQEQ || op == KtTokens.EXCLEQEQEQ) {
|
|
714
|
+
boolean eq = op == KtTokens.EQEQ || op == KtTokens.EQEQEQ;
|
|
715
|
+
if (r.t() != null && r.t().is("null")) return new X(cmp(eq ? "is" : "is not", l.ir(), r.ir(), p, t), new JT("boolean"));
|
|
716
|
+
if (l.t() != null && l.t().is("null")) return new X(cmp(eq ? "is" : "is not", r.ir(), l.ir(), p, t), new JT("boolean"));
|
|
717
|
+
Map<String, Object> a = l.ir(), bb = r.ir();
|
|
718
|
+
if (num(l.t()) && num(r.t())) { JT w = promote(l.t(), r.t()); a = at(a, l.t(), w); bb = at(bb, r.t(), w); }
|
|
719
|
+
return new X(cmp(eq ? "==" : "!=", a, bb, p, t), new JT("boolean"));
|
|
720
|
+
}
|
|
721
|
+
String rel = op == KtTokens.LT ? "<" : op == KtTokens.LTEQ ? "<=" : op == KtTokens.GT ? ">" : op == KtTokens.GTEQ ? ">=" : null;
|
|
722
|
+
if (rel != null) return new X(cmp(rel, l.ir(), r.ir(), p, t), new JT("boolean"));
|
|
723
|
+
String a = op == KtTokens.PLUS ? "+" : op == KtTokens.MINUS ? "-" : op == KtTokens.MUL ? "*" : op == KtTokens.DIV ? "/" : op == KtTokens.PERC ? "%" : null;
|
|
724
|
+
if (a != null) {
|
|
725
|
+
if (l.t() != null && l.t().is("java.lang.String")) return new X(x("bin", p, "op", "+", "x", l.ir(), "y", r.ir(), "text", t), l.t());
|
|
726
|
+
if (!num(l.t()) || !num(r.t())) return no1(b, "an operator of other than numbers");
|
|
727
|
+
JT ty = promote(l.t(), r.t());
|
|
728
|
+
if (floating(ty) && (a.equals("/") || a.equals("%"))) return no1(b, "a float division (the subset reads whole numbers)");
|
|
729
|
+
return new X(arith(a, l.ir(), l.t(), r.ir(), r.t(), ty, "wrap", p, t), ty);
|
|
730
|
+
}
|
|
731
|
+
if (op == KtTokens.IDENTIFIER) {
|
|
732
|
+
String n = b.getOperationReference().getReferencedName();
|
|
733
|
+
if ((n.equals("shl") || n.equals("shr")) && integral(l.t())) return new X(x("binop", p, "op", n.equals("shl") ? "<<" : ">>", "x", at(l.ir(), l.t(), l.t()), "y", r.ir(), "text", t), l.t());
|
|
734
|
+
if ((n.equals("and") || n.equals("or")) && "boolean".equals(prim(l.t()))) return new X(x(n, p, "xs", List.of(l.ir(), r.ir())), new JT("boolean"));
|
|
735
|
+
return no1(b, n + " (an infix function outside the subset)");
|
|
736
|
+
}
|
|
737
|
+
return no1(b, "an operator outside the subset");
|
|
738
|
+
}
|
|
739
|
+
X name(KtNameReferenceExpression n, Ctx c, String p, String t) {
|
|
740
|
+
String id = n.getReferencedName();
|
|
741
|
+
if (c.locals.containsKey(id)) return new X(var(id, p), c.locals.get(id));
|
|
742
|
+
JT ft = field(id);
|
|
743
|
+
if (ft != null) return new X(get(self(p), id, p, id), ft);
|
|
744
|
+
KtProperty cp = computedProp(id);
|
|
745
|
+
if (cp != null) return new X(Jvm.call(get(self(p), id, p, id), List.of(), p, t), type(cp.getTypeReference()));
|
|
746
|
+
KtProperty k = constant(id);
|
|
747
|
+
if (k != null && k.getInitializer() != null) return ex(k.getInitializer(), c.fresh(), type(k.getTypeReference()));
|
|
748
|
+
return new X(no(p, t + ": a name the reader can't resolve (another file's, or other code's)"), null);
|
|
749
|
+
}
|
|
750
|
+
// A receiver's member: an enum's constant, a field of an object of the code's own, a computed property, a call.
|
|
751
|
+
X dot(KtDotQualifiedExpression d, Ctx c, String p, String t, JT want, boolean stmt) {
|
|
752
|
+
KtExpression r = d.getReceiverExpression(), sel = d.getSelectorExpression();
|
|
753
|
+
// A qualified name: an enum's constant, a companion's constant, a JDK class's static call
|
|
754
|
+
String qual = r.getText().replaceAll("\\s", "");
|
|
755
|
+
if (sel instanceof KtNameReferenceExpression sn && qual.matches("[A-Z][\\w.]*") && !c.locals.containsKey(qual) && field(qual) == null) {
|
|
756
|
+
String ty = resolve(qual);
|
|
757
|
+
List<String> ev = enumValues(ty);
|
|
758
|
+
if (ev != null && ev.contains(sn.getReferencedName())) return new X(str(sn.getReferencedName(), p), new JT(ty));
|
|
759
|
+
String q = ty + "." + sn.getReferencedName();
|
|
760
|
+
if (q.equals("java.lang.Integer.MAX_VALUE") || q.equals("int.MAX_VALUE")) return new X(lit(Integer.MAX_VALUE, new JT("int"), p), new JT("int"));
|
|
761
|
+
if (q.equals("java.lang.Integer.MIN_VALUE") || q.equals("int.MIN_VALUE")) return new X(lit(Integer.MIN_VALUE, new JT("int"), p), new JT("int"));
|
|
762
|
+
if (q.equals("long.MAX_VALUE") || q.equals("long.MIN_VALUE")) return no1(d, "Long's bound (past the rule's ±2^53)");
|
|
763
|
+
return no1(d, "a static member of another class");
|
|
764
|
+
}
|
|
765
|
+
if (sel instanceof KtCallExpression ce && qual.matches("[A-Z][\\w.]*") && !c.locals.containsKey(qual) && field(qual) == null) return call(ce, null, new JT(resolve(qual)), c, p, t, want, stmt);
|
|
766
|
+
X o = ex(r, c);
|
|
767
|
+
if (sel instanceof KtCallExpression ce) return call(ce, o, null, c, p, t, want, stmt);
|
|
768
|
+
if (!(sel instanceof KtNameReferenceExpression sn)) return no1(d, "a member outside the subset");
|
|
769
|
+
String f = sn.getReferencedName();
|
|
770
|
+
if (o.t() != null && atomic(o.t()) && f.equals("value")) return new X(o.ir(), held(o.t()));
|
|
771
|
+
if (o.t() != null && map(o.t()) && f.equals("size")) return no1(d, "a map's size");
|
|
772
|
+
if (o.t() != null && (o.t().n.equals(selfName) || chain(self).stream().anyMatch(k -> binOf(k).equals(o.t().n)))) {
|
|
773
|
+
JT ft = field(f);
|
|
774
|
+
if (ft != null) return new X(get(o.ir(), f, p, t), ft);
|
|
775
|
+
KtProperty cp = computedProp(f);
|
|
776
|
+
if (cp != null) return new X(Jvm.call(get(o.ir(), f, p, t), List.of(), p, t), type(cp.getTypeReference()));
|
|
777
|
+
}
|
|
778
|
+
if (o.t() != null && rec(o.t().n) != null && classes.get(o.t().n) instanceof KtClass kc) {
|
|
779
|
+
for (KtParameter pa : kc.getPrimaryConstructorParameters()) if (f.equals(pa.getName())) return new X(get(o.ir(), f, p, t), type(pa.getTypeReference()));
|
|
780
|
+
}
|
|
781
|
+
return no1(d, "a property of other code (" + (o.t() == null ? "?" : o.t().simple()) + ")");
|
|
782
|
+
}
|
|
783
|
+
// x?.f and x?.let { }: the member where x isn't null, null where it is.
|
|
784
|
+
X safe(KtSafeQualifiedExpression sq, Ctx c, String p, String t) {
|
|
785
|
+
if (c.cond > 0) return no1(sq, "?. inside a condition");
|
|
786
|
+
X o = ex(sq.getReceiverExpression(), c);
|
|
787
|
+
String v = "$safe" + (tmp++);
|
|
788
|
+
c.pre.add(s("set", p, "to", var(v, p), "x", o.ir()));
|
|
789
|
+
c.locals.put(v, o.t() == null ? null : o.t().nullable(false));
|
|
790
|
+
Ctx in = new Ctx(new ArrayList<>(), c.locals, 0, c.init);
|
|
791
|
+
KtExpression sel = sq.getSelectorExpression();
|
|
792
|
+
X inner = sel instanceof KtCallExpression ce ? call(ce, new X(var(v, p), o.t() == null ? null : o.t().nullable(false)), null, in, p, t, null, false) : no1(sq, "?. of a property");
|
|
793
|
+
if (!in.pre.isEmpty()) {
|
|
794
|
+
String r = "$safev" + (tmp++);
|
|
795
|
+
List<Object> then = new ArrayList<>(in.pre);
|
|
796
|
+
then.add(s("set", p, "to", var(r, p), "x", inner.ir()));
|
|
797
|
+
c.pre.add(s("set", p, "to", var(r, p), "x", x("none", p)));
|
|
798
|
+
c.pre.add(s("if", p, "c", cmp("is not", var(v, p), x("none", p), p, t), "then", then, "else", List.of()));
|
|
799
|
+
return new X(var(r, p), inner.t() == null ? null : inner.t().nullable(true));
|
|
800
|
+
}
|
|
801
|
+
return new X(x("ite", p, "c", cmp("is not", var(v, p), x("none", p), p, t), "a", inner.ir(), "b", x("none", p)), inner.t() == null ? null : inner.t().nullable(true));
|
|
802
|
+
}
|
|
803
|
+
// An expression used as a statement: a call whose value no one reads (a scope function's body runs as statements).
|
|
804
|
+
X exStmt(KtExpression e, Ctx c, JT ret) {
|
|
805
|
+
if (e instanceof KtDotQualifiedExpression d) return dot(d, c, where(e), text(e), null, true);
|
|
806
|
+
if (e instanceof KtCallExpression ce) return call(ce, null, null, c, where(e), text(e), null, true);
|
|
807
|
+
if (e instanceof KtSafeQualifiedExpression sq && sq.getSelectorExpression() instanceof KtCallExpression ce && lambda(ce) != null) {
|
|
808
|
+
String p = where(e);
|
|
809
|
+
X o = ex(sq.getReceiverExpression(), c);
|
|
810
|
+
String v = "$safe" + (tmp++);
|
|
811
|
+
c.pre.add(s("set", p, "to", var(v, p), "x", o.ir()));
|
|
812
|
+
Ctx in = new Ctx(new ArrayList<>(), new HashMap<>(c.locals), 0, c.init);
|
|
813
|
+
in.locals.put(v, o.t() == null ? null : o.t().nullable(false));
|
|
814
|
+
X body = call(ce, new X(var(v, p), o.t() == null ? null : o.t().nullable(false)), null, in, p, text(e), null, true);
|
|
815
|
+
List<Object> then = new ArrayList<>(in.pre);
|
|
816
|
+
if ("$stmts".equals(body.ir().get("e"))) then.addAll((List<?>) body.ir().get("ss")); else then.add(s("expr", p, "x", body.ir()));
|
|
817
|
+
return new X(x("$stmts", p, "ss", List.of(s("if", p, "c", cmp("is not", var(v, p), x("none", p), p, text(e)), "then", then, "else", List.of()))), null);
|
|
818
|
+
}
|
|
819
|
+
return ex(e, c);
|
|
820
|
+
}
|
|
821
|
+
static KtLambdaExpression lambda(KtCallExpression ce) { return ce.getLambdaArguments().isEmpty() ? null : ce.getLambdaArguments().get(0).getLambdaExpression(); }
|
|
822
|
+
// A lambda inlined (a scope function's, synchronized's): its parameter (or `it`) bound to a value, its statements
|
|
823
|
+
// before, its last expression its value.
|
|
824
|
+
X inline(KtLambdaExpression l, String it, X v, Ctx c, boolean stmt, String p) {
|
|
825
|
+
if (c.cond > 0) return no1(l, "a scope function inside a condition");
|
|
826
|
+
String name = l.getValueParameters().isEmpty() ? "it" : l.getValueParameters().get(0).getName();
|
|
827
|
+
String bound = "$it" + (tmp++);
|
|
828
|
+
if (v != null) { c.pre.add(s("set", p, "to", var(bound, p), "x", v.ir())); }
|
|
829
|
+
Ctx in = new Ctx(c.pre, new HashMap<>(c.locals), 0, c.init);
|
|
830
|
+
if (v != null) { in.locals.put(bound, v.t()); in.locals.put(name, v.t()); c.pre.add(s("set", p, "to", var(name, p), "x", var(bound, p))); }
|
|
831
|
+
List<KtExpression> ss = l.getBodyExpression() == null ? List.of() : l.getBodyExpression().getStatements();
|
|
832
|
+
for (int i = 0; i < ss.size() - 1; i++) c.pre.addAll(stmt(ss.get(i), in, null));
|
|
833
|
+
if (ss.isEmpty()) return new X(x("none", p), new JT("void"));
|
|
834
|
+
KtExpression last = ss.get(ss.size() - 1);
|
|
835
|
+
if (stmt) { c.pre.addAll(stmt(last, in, null)); return new X(x("none", p), new JT("void")); }
|
|
836
|
+
return ex(last, in);
|
|
837
|
+
}
|
|
838
|
+
// A call: the class's own function (inlined), a data class's copy, the standard library's (lowered), a scope
|
|
839
|
+
// function (inlined), or other code's (`ext`).
|
|
840
|
+
X call(KtCallExpression ce, X recv, JT staticOwner, Ctx c, String p, String t, JT want, boolean stmt) {
|
|
841
|
+
String n = ce.getCalleeExpression() == null ? "?" : ce.getCalleeExpression().getText();
|
|
842
|
+
KtLambdaExpression lam = lambda(ce);
|
|
843
|
+
List<KtValueArgument> vas = ce.getValueArguments().stream().filter(a -> !(a instanceof KtLambdaArgument)).map(a -> (KtValueArgument) a).toList();
|
|
844
|
+
// synchronized(x) { }, x.withLock { }: the step atomic
|
|
845
|
+
if ((recv == null && n.equals("synchronized") && lam != null) || (recv != null && n.equals("withLock") && lam != null)) {
|
|
846
|
+
Ctx in = new Ctx(new ArrayList<>(), new HashMap<>(c.locals), 0, c.init);
|
|
847
|
+
List<Object> body = new ArrayList<>();
|
|
848
|
+
for (KtExpression s : lam.getBodyExpression().getStatements()) body.addAll(stmt(s, in, null));
|
|
849
|
+
if (!stmt) return no1(ce, "synchronized as a value");
|
|
850
|
+
return new X(x("$stmts", p, "ss", List.of(with(body, p, t))), null);
|
|
851
|
+
}
|
|
852
|
+
// scope functions
|
|
853
|
+
if (recv != null && lam != null && vas.isEmpty() && Set.of("let", "also", "run", "apply", "takeIf", "takeUnless").contains(n)) {
|
|
854
|
+
if (n.equals("run") || n.equals("apply")) return no1(ce, n + " (a lambda with a receiver)");
|
|
855
|
+
if (n.equals("takeIf") || n.equals("takeUnless")) {
|
|
856
|
+
String v = "$take" + (tmp++);
|
|
857
|
+
c.pre.add(s("set", p, "to", var(v, p), "x", recv.ir()));
|
|
858
|
+
c.locals.put(v, recv.t());
|
|
859
|
+
X pred = inline(lam, null, new X(var(v, p), recv.t()), c, false, p);
|
|
860
|
+
Map<String, Object> cnd = n.equals("takeIf") ? pred.ir() : not(pred.ir(), p);
|
|
861
|
+
return new X(x("ite", p, "c", cnd, "a", var(v, p), "b", x("none", p)), recv.t() == null ? null : recv.t().nullable(true));
|
|
862
|
+
}
|
|
863
|
+
X body = inline(lam, null, recv, c, stmt && n.equals("let"), p);
|
|
864
|
+
return n.equals("also") ? recv : body;
|
|
865
|
+
}
|
|
866
|
+
List<Map<String, Object>> args = new ArrayList<>();
|
|
867
|
+
List<JT> argT = new ArrayList<>();
|
|
868
|
+
List<Object> kw = new ArrayList<>();
|
|
869
|
+
boolean pure = true;
|
|
870
|
+
// the class's own function: its parameters' types given to the arguments
|
|
871
|
+
KtNamedFunction own = recv == null && staticOwner == null ? method(n, vas.size()) : recv != null && recv.t() != null && (recv.t().n.equals(selfName) || chain(self).stream().anyMatch(k -> binOf(k).equals(recv.t().n))) ? method(n, vas.size()) : null;
|
|
872
|
+
for (int i = 0; i < vas.size(); i++) {
|
|
873
|
+
KtValueArgument a = vas.get(i);
|
|
874
|
+
JT pt = own != null && i < own.getValueParameters().size() ? type(own.getValueParameters().get(i).getTypeReference()) : null;
|
|
875
|
+
X v = ex(a.getArgumentExpression(), c.under(), pt);
|
|
876
|
+
pure &= v.ir().toString().indexOf("call") < 0;
|
|
877
|
+
if (a.getArgumentName() != null) { kw.add(List.of(a.getArgumentName().getAsName().asString(), v.ir())); continue; }
|
|
878
|
+
args.add(pt != null && num(pt) ? at(v.ir(), v.t(), pt) : v.ir());
|
|
879
|
+
argT.add(pt != null && num(pt) ? pt : v.t());
|
|
880
|
+
}
|
|
881
|
+
if (own != null) {
|
|
882
|
+
KtClassOrObject k = ownerOf(own);
|
|
883
|
+
Map<String, Object> f = get(recv == null ? self(p) : recv.ir(), key(k, own), p, n);
|
|
884
|
+
return new X(m("e", "call", "p", p, "text", t, "f", f, "args", args, "kw", kw), type(own.getTypeReference()));
|
|
885
|
+
}
|
|
886
|
+
if (recv == null && staticOwner == null && n.equals("copy") || (recv != null && n.equals("copy") && recv.t() != null && classes.get(recv.t().n) instanceof KtClass kc && kc.isData())) {
|
|
887
|
+
if (!args.isEmpty()) return no1(ce, "copy given arguments by position");
|
|
888
|
+
List<Object> fs = new ArrayList<>();
|
|
889
|
+
for (Object k0 : kw) fs.add(k0);
|
|
890
|
+
return new X(x("upd", p, "x", recv == null ? self(p) : recv.ir(), "fields", fs, "text", t), recv == null ? new JT(selfName) : recv.t());
|
|
891
|
+
}
|
|
892
|
+
// a constructor: a class of the code's own (made from its fields), an atomic, a map, a lock
|
|
893
|
+
String ctorOf = recv == null && Character.isUpperCase(n.charAt(0)) ? resolve(n) : null;
|
|
894
|
+
List<JT> targs = new ArrayList<>();
|
|
895
|
+
for (KtTypeProjection tp : ce.getTypeArguments()) { JT a = type(tp.getTypeReference()); targs.add(a == null ? new JT("java.lang.Object") : a.nul ? a : boxed(a)); }
|
|
896
|
+
if (ctorOf != null) {
|
|
897
|
+
JT ty = new JT(ctorOf, targs, false);
|
|
898
|
+
if (atomic(ty)) return new X(args.isEmpty() ? lit(0, held(ty), p) : at(args.get(0), argT.get(0), held(ty)), ty);
|
|
899
|
+
if (map(ty)) return new X(x("dict", p, "keys", List.of(), "vals", List.of(), "text", t), ty);
|
|
900
|
+
if (LOCKS.contains(ty.n) || ty.is("java.lang.Object")) return new X(lock(p, t), ty);
|
|
901
|
+
String r = rec(ctorOf);
|
|
902
|
+
if (r != null) return new X(m("e", "call", "p", p, "text", t, "f", x("cls", p, "rec", r), "args", args, "kw", kw), ty);
|
|
903
|
+
if (ctorOf.equals(selfName)) return no1(ce, "an object of the class read made inside it");
|
|
904
|
+
}
|
|
905
|
+
if (recv == null && (n.equals("mutableMapOf") || n.equals("hashMapOf") || n.equals("linkedMapOf")) && args.isEmpty()) return new X(x("dict", p, "keys", List.of(), "vals", List.of(), "text", t), new JT("java.util.Map", targs, false));
|
|
906
|
+
if (recv == null && n.equals("atomic") && args.size() == 1) return new X(args.get(0), new JT("kotlinx.atomicfu.Atomic" + (argT.get(0) != null && "long".equals(prim(argT.get(0))) ? "Long" : "Int")));
|
|
907
|
+
if (recv != null && recv.t() != null && integral(recv.t()) && args.size() <= 2) {
|
|
908
|
+
JT rt = recv.t();
|
|
909
|
+
switch (n) {
|
|
910
|
+
case "coerceAtLeast": return new X(x("minmax", p, "op", "max", "xs", List.of(at(recv.ir(), rt, rt), at(args.get(0), argT.get(0), rt)), "ity", ity(rt), "text", t), rt);
|
|
911
|
+
case "coerceAtMost": return new X(x("minmax", p, "op", "min", "xs", List.of(at(recv.ir(), rt, rt), at(args.get(0), argT.get(0), rt)), "ity", ity(rt), "text", t), rt);
|
|
912
|
+
case "coerceIn": if (args.size() == 2) return new X(x("minmax", p, "op", "max", "xs", List.of(at(args.get(0), argT.get(0), rt), x("minmax", p, "op", "min", "xs", List.of(at(recv.ir(), rt, rt), at(args.get(1), argT.get(1), rt)), "ity", ity(rt))), "ity", ity(rt), "text", t), rt); break;
|
|
913
|
+
case "toLong": return new X(at(recv.ir(), rt, new JT("long")), new JT("long"));
|
|
914
|
+
case "toInt": return "int".equals(prim(rt)) ? recv : new X(x("conv", p, "x", recv.ir(), "ity", ity(new JT("int")), "why", t), new JT("int"));
|
|
915
|
+
case "absoluteValue": break;
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
if (recv == null && (n.equals("minOf") || n.equals("maxOf")) && args.size() == 2) { JT w = promote(argT.get(0), argT.get(1)); return new X(x("minmax", p, "op", n.equals("minOf") ? "min" : "max", "xs", List.of(at(args.get(0), argT.get(0), w), at(args.get(1), argT.get(1), w)), "ity", ity(w), "why", t, "text", t), w); }
|
|
919
|
+
if (recv == null && n.equals("TODO")) return stmt ? new X(x("$stmts", p, "ss", List.of(fail(p, t + " (a NotImplementedError)"))), null) : no1(ce, "TODO as a value");
|
|
920
|
+
// the library, through the JVM layer
|
|
921
|
+
String owner = staticOwner != null ? staticOwner.n : recv == null ? "kotlin.StandardKt" : recv.t() == null ? "?" : recv.t().n;
|
|
922
|
+
JT out = want;
|
|
923
|
+
if (recv != null && recv.t() != null && map(recv.t())) out = n.equals("get") || n.equals("remove") || n.equals("put") ? recv.t().arg(1).nullable(true) : n.equals("getOrDefault") || n.equals("getValue") ? recv.t().arg(1) : n.equals("containsKey") ? new JT("boolean") : want;
|
|
924
|
+
if (recv != null && recv.t() != null && atomic(recv.t())) out = Set.of("compareAndSet", "weakCompareAndSet").contains(n) ? new JT("boolean") : held(recv.t());
|
|
925
|
+
if (staticOwner != null && staticOwner.is("java.lang.System")) out = new JT("long");
|
|
926
|
+
if ((n.equals("check") || n.equals("require") || n.equals("error")) && recv == null) out = new JT("void");
|
|
927
|
+
Low l = lib(new Jvm.Call(owner, n, recv == null ? null : recv.ir(), recv == null ? staticOwner : recv.t(), args, argT, out, p, t, stmt, pure));
|
|
928
|
+
if (l != null) {
|
|
929
|
+
if (l.x() != null && "clock".equals(l.x().get("e"))) rewrites.add(m("span", span(ce.getParent() instanceof KtDotQualifiedExpression dq && dq.getSelectorExpression() == ce ? dq : ce), "to", "now"));
|
|
930
|
+
if (l.ss() != null) return new X(x("$stmts", p, "ss", l.ss()), null);
|
|
931
|
+
JT rt = out;
|
|
932
|
+
if ("expect".equals(l.x().get("e")) && !args.isEmpty()) rt = argT.get(0) == null ? null : argT.get(0).nullable(false);
|
|
933
|
+
return new X(l.x(), rt);
|
|
934
|
+
}
|
|
935
|
+
PsiElement site = ce.getParent() instanceof KtDotQualifiedExpression dq && dq.getSelectorExpression() == ce ? dq : ce;
|
|
936
|
+
JT dt = declared(owner, n, args.size() + kw.size());
|
|
937
|
+
if (dt != null) want = dt;
|
|
938
|
+
String callee = recv == null || !(site instanceof KtDotQualifiedExpression dq2) ? n : text(dq2.getReceiverExpression()).replace("this.", "") + "." + n;
|
|
939
|
+
// (a method of an object of other code the class holds or is given: called on it, so an injected dependency is
|
|
940
|
+
// read as one; another call of other code: `ext`, its declared result the answer's type)
|
|
941
|
+
Map<String, Object> f = recv != null && recv.t() != null && other(recv.t().n) != null ? get(recv.ir(), n, p, callee) : x("ext", p, "q", owner + "." + n, "text", callee, "patch", m("mod", owner, "path", List.of(n)), "ret", want == null ? null : stype(want, this));
|
|
942
|
+
return new X(m("e", "call", "p", p, "text", t, "f", f, "args", args, "kw", kw,
|
|
943
|
+
"jt", String.valueOf(want), "ity", ity(want), "decl", owner, "iface", iface(owner) ? true : null, "span", span(site), "tag", tag(site)), want);
|
|
944
|
+
}
|
|
945
|
+
// Other code's function's declared result: a class of the code's sources read (its declaration), or one on the
|
|
946
|
+
// reader's classpath (by reflection).
|
|
947
|
+
JT declared(String owner, String n, int arity) {
|
|
948
|
+
KtClassOrObject k = classes.get(owner);
|
|
949
|
+
if (k != null) for (KtDeclaration d : body(k)) if (d instanceof KtNamedFunction f && n.equals(f.getName()) && f.getValueParameters().size() == arity) return type(f.getTypeReference());
|
|
950
|
+
Class<?> c = load(owner);
|
|
951
|
+
if (c != null) for (java.lang.reflect.Method m : c.getMethods()) if (m.getName().equals(n) && m.getParameterCount() == arity) return jtOf(m.getReturnType(), m.getGenericReturnType());
|
|
952
|
+
return null;
|
|
953
|
+
}
|
|
954
|
+
static JT jtOf(Class<?> c, java.lang.reflect.Type g) {
|
|
955
|
+
List<JT> as = new ArrayList<>();
|
|
956
|
+
if (g instanceof java.lang.reflect.ParameterizedType pt) for (java.lang.reflect.Type a : pt.getActualTypeArguments()) as.add(a instanceof Class<?> ac ? jtOf(ac, ac) : new JT("java.lang.Object"));
|
|
957
|
+
return new JT(c.getName(), as, false);
|
|
958
|
+
}
|
|
959
|
+
boolean iface(String owner) { KtClassOrObject k = classes.get(owner); if (k instanceof KtClass kc) return kc.isInterface(); Class<?> c = load(owner); return c != null && c.isInterface(); }
|
|
960
|
+
}
|