gdp-wired 0.31.0 → 0.32.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.
Files changed (73) hide show
  1. package/README.md +82 -6
  2. package/dist/core/c/reader.d.ts +86 -0
  3. package/dist/core/c/reader.js +860 -0
  4. package/dist/core/cli.d.ts +66 -0
  5. package/dist/core/cli.js +582 -0
  6. package/dist/core/dsl.d.ts +4 -2
  7. package/dist/core/dsl.js +22 -12
  8. package/dist/core/effect.d.ts +6 -4
  9. package/dist/core/effect.js +48 -10
  10. package/dist/core/ex/harness.exs +177 -0
  11. package/dist/core/ex/json.exs +46 -0
  12. package/dist/core/ex/reader.exs +462 -0
  13. package/dist/core/exists.d.ts +3 -0
  14. package/dist/core/exists.js +3 -1
  15. package/dist/core/extract-c.d.ts +35 -0
  16. package/dist/core/extract-c.js +424 -0
  17. package/dist/core/extract-check.js +27 -6
  18. package/dist/core/extract-ex.d.ts +16 -0
  19. package/dist/core/extract-ex.js +33 -0
  20. package/dist/core/extract-go-check.d.ts +28 -0
  21. package/dist/core/extract-go-check.js +639 -0
  22. package/dist/core/extract-go.d.ts +231 -0
  23. package/dist/core/extract-go.js +1166 -0
  24. package/dist/core/extract-jvm.d.ts +23 -0
  25. package/dist/core/extract-jvm.js +220 -0
  26. package/dist/core/extract-print.js +17 -7
  27. package/dist/core/extract-py.d.ts +15 -0
  28. package/dist/core/extract-py.js +31 -0
  29. package/dist/core/extract-rb.d.ts +17 -0
  30. package/dist/core/extract-rb.js +31 -0
  31. package/dist/core/extract-read.d.ts +20 -0
  32. package/dist/core/extract-read.js +46 -6
  33. package/dist/core/extract-rs.d.ts +12 -0
  34. package/dist/core/extract-rs.js +306 -0
  35. package/dist/core/extract-shapes.d.ts +2 -0
  36. package/dist/core/extract-shapes.js +6 -5
  37. package/dist/core/extract-step.d.ts +806 -0
  38. package/dist/core/extract-step.js +4141 -0
  39. package/dist/core/extract-swift.d.ts +27 -0
  40. package/dist/core/extract-swift.js +484 -0
  41. package/dist/core/extract-xstate.d.ts +10 -3
  42. package/dist/core/extract-xstate.js +50 -4
  43. package/dist/core/extract.d.ts +22 -3
  44. package/dist/core/extract.js +107 -18
  45. package/dist/core/go/go.mod +3 -0
  46. package/dist/core/go/reader.go +901 -0
  47. package/dist/core/infer.js +1770 -52
  48. package/dist/core/jvm/Harness.java +361 -0
  49. package/dist/core/jvm/JavaReader.java +825 -0
  50. package/dist/core/jvm/Jvm.java +332 -0
  51. package/dist/core/jvm/KtReader.java +960 -0
  52. package/dist/core/jvm/Rt.java +10 -0
  53. package/dist/core/laws.d.ts +66 -0
  54. package/dist/core/laws.js +522 -0
  55. package/dist/core/py/harness.py +328 -0
  56. package/dist/core/py/reader.py +447 -0
  57. package/dist/core/rb/harness.rb +233 -0
  58. package/dist/core/rb/reader.rb +375 -0
  59. package/dist/core/rs/Cargo.lock +108 -0
  60. package/dist/core/rs/Cargo.toml +14 -0
  61. package/dist/core/rs/src/main.rs +681 -0
  62. package/dist/core/swift/Harness.swift +124 -0
  63. package/dist/core/swift/Main.swift +34 -0
  64. package/dist/core/swift/Package.swift +13 -0
  65. package/dist/core/swift/Sources/gdp-swiftreader/main.swift +1341 -0
  66. package/dist/kernel/cdcl.js +43 -18
  67. package/dist/kernel/epr.d.ts +35 -0
  68. package/dist/kernel/epr.js +1676 -0
  69. package/dist/kernel/prove.d.ts +44 -0
  70. package/dist/kernel/prove.js +668 -54
  71. package/dist/lean-kernel/emit.d.ts +1 -0
  72. package/dist/lean-kernel/emit.js +21 -4
  73. package/package.json +53 -6
@@ -0,0 +1,332 @@
1
+ // The JVM readers' shared part (core/jvm/JavaReader.java and KtReader.java, core/extract-jvm.ts): JSON, the JVM types
2
+ // both languages read into (one canonical name per type: Kotlin's Int is Java's int, its Int? an Integer that may be
3
+ // null), each type's place in the shared step IR (core/extract-step.ts's SType), and the library calls lowered the
4
+ // same way from either language: maps, Optional and nullability, the atomics, Math's exact arithmetic, TimeUnit's
5
+ // conversions, the clock, the checks that throw. Each reader resolves a call to its declaring type and hands it here;
6
+ // what isn't known is other code (`ext`, answered or skipped by the caller), refused where a step reaches it.
7
+ package gdp.jvm;
8
+
9
+ import java.util.*;
10
+
11
+ public final class Jvm {
12
+ // --- JSON -----------------------------------------------------------------------------------------------------------
13
+ static Object parse(String s) { int[] i = {0}; Object o = val(s, i); return o; }
14
+ private static void ws(String s, int[] i) { while (i[0] < s.length() && Character.isWhitespace(s.charAt(i[0]))) i[0]++; }
15
+ private static Object val(String s, int[] i) {
16
+ ws(s, i);
17
+ char c = s.charAt(i[0]);
18
+ if (c == '{') {
19
+ Map<String, Object> m = new LinkedHashMap<>(); i[0]++; ws(s, i);
20
+ if (s.charAt(i[0]) == '}') { i[0]++; return m; }
21
+ for (;;) { ws(s, i); String k = (String) val(s, i); ws(s, i); i[0]++; m.put(k, val(s, i)); ws(s, i); if (s.charAt(i[0]++) == '}') return m; }
22
+ }
23
+ if (c == '[') {
24
+ List<Object> l = new ArrayList<>(); i[0]++; ws(s, i);
25
+ if (s.charAt(i[0]) == ']') { i[0]++; return l; }
26
+ for (;;) { l.add(val(s, i)); ws(s, i); if (s.charAt(i[0]++) == ']') return l; }
27
+ }
28
+ if (c == '"') {
29
+ StringBuilder b = new StringBuilder(); i[0]++;
30
+ for (;;) {
31
+ char d = s.charAt(i[0]++);
32
+ if (d == '"') return b.toString();
33
+ if (d != '\\') { b.append(d); continue; }
34
+ char e = s.charAt(i[0]++);
35
+ switch (e) {
36
+ case 'n' -> b.append('\n'); case 't' -> b.append('\t'); case 'r' -> b.append('\r'); case 'b' -> b.append('\b'); case 'f' -> b.append('\f');
37
+ case 'u' -> { b.append((char) Integer.parseInt(s.substring(i[0], i[0] + 4), 16)); i[0] += 4; }
38
+ default -> b.append(e);
39
+ }
40
+ }
41
+ }
42
+ if (s.startsWith("true", i[0])) { i[0] += 4; return true; }
43
+ if (s.startsWith("false", i[0])) { i[0] += 5; return false; }
44
+ if (s.startsWith("null", i[0])) { i[0] += 4; return null; }
45
+ int j = i[0];
46
+ while (i[0] < s.length() && "+-0123456789.eE".indexOf(s.charAt(i[0])) >= 0) i[0]++;
47
+ String n = s.substring(j, i[0]);
48
+ return n.contains(".") || n.contains("e") || n.contains("E") ? (Object) Double.parseDouble(n) : (Object) Long.parseLong(n);
49
+ }
50
+ static String json(Object o) { StringBuilder b = new StringBuilder(); json(o, b); return b.toString(); }
51
+ static void json(Object o, StringBuilder b) {
52
+ if (o == null) b.append("null");
53
+ else if (o instanceof String s) {
54
+ b.append('"');
55
+ for (char c : s.toCharArray()) {
56
+ if (c == '"' || c == '\\') b.append('\\').append(c);
57
+ else if (c == '\n') b.append("\\n"); else if (c == '\t') b.append("\\t"); else if (c == '\r') b.append("\\r");
58
+ else if (c < 0x20) b.append(String.format("\\u%04x", (int) c)); else b.append(c);
59
+ }
60
+ b.append('"');
61
+ } else if (o instanceof Map<?, ?> m) {
62
+ b.append('{'); boolean first = true;
63
+ for (Map.Entry<?, ?> e : m.entrySet()) { if (!first) b.append(','); first = false; json(String.valueOf(e.getKey()), b); b.append(':'); json(e.getValue(), b); }
64
+ b.append('}');
65
+ } else if (o instanceof Collection<?> l) {
66
+ b.append('['); boolean first = true;
67
+ for (Object x : l) { if (!first) b.append(','); first = false; json(x, b); }
68
+ b.append(']');
69
+ } else b.append(o);
70
+ }
71
+
72
+ // --- the IR's nodes -------------------------------------------------------------------------------------------------
73
+ static Map<String, Object> m(Object... kv) { Map<String, Object> o = new LinkedHashMap<>(); for (int i = 0; i + 1 < kv.length; i += 2) if (kv[i + 1] != null) o.put((String) kv[i], kv[i + 1]); return o; }
74
+ static Map<String, Object> x(String e, String p, Object... kv) { Map<String, Object> o = m(kv); o.put("e", e); o.put("p", p); return o; }
75
+ static Map<String, Object> s(String s, String p, Object... kv) { Map<String, Object> o = m(kv); o.put("s", s); o.put("p", p); return o; }
76
+ static Map<String, Object> no(String p, String why) { return x("no", p, "why", why); }
77
+ static Map<String, Object> sno(String p, String why) { return s("no", p, "why", why); }
78
+ static Map<String, Object> var(String n, String p) { return x("var", p, "n", n); }
79
+ static Map<String, Object> self(String p) { return var("self", p); }
80
+ static Map<String, Object> get(Map<String, Object> o, String f, String p, String text) { return x("get", p, "x", o, "f", f, "text", text); }
81
+ static Map<String, Object> call(Map<String, Object> f, List<?> args, String p, String text) { return x("call", p, "f", f, "args", args, "kw", List.of(), "text", text); }
82
+ static Map<String, Object> lit(long v, JT t, String p) { return x("int", p, "v", Long.toString(v), "ity", ity(t)); }
83
+ static Map<String, Object> str(String v, String p) { return x("str", p, "v", v); }
84
+ static Map<String, Object> cmp(String op, Object a, Object b, String p, String text) { return x("cmp", p, "op", op, "x", a, "y", b, "text", text); }
85
+ static Map<String, Object> not(Object a, String p) { return x("not", p, "x", a); }
86
+ static Map<String, Object> fail(String p, String why) { return s("fail", p, "why", why); }
87
+ // A block `break` leaves (Go's switch IR with one default case): a switch's arms, a compare-and-set loop's one pass.
88
+ static Map<String, Object> breakable(List<Object> body, String p) { return s("switch", p, "cases", List.of(m("default", true, "b", body, "p", p))); }
89
+ static Map<String, Object> lock(String p, String text) { return call(x("lib", p, "q", "threading.Lock"), List.of(), p, text); }
90
+ static Map<String, Object> with(List<Object> body, String p, String text) { return s("with", p, "items", List.of(m("x", lock(p, text))), "body", body); }
91
+
92
+ // --- JVM types: one canonical name per type (Kotlin's mapped onto Java's) -------------------------------------------
93
+ static final class JT {
94
+ final String n; final List<JT> a; final boolean nul;
95
+ JT(String n, List<JT> a, boolean nul) { this.n = n; this.a = a; this.nul = nul; }
96
+ JT(String n) { this(n, List.of(), false); }
97
+ JT nullable(boolean b) { return b == nul ? this : new JT(n, a, b); }
98
+ JT arg(int i) { return i < a.size() ? a.get(i) : new JT("java.lang.Object"); }
99
+ boolean is(String... ns) { for (String x : ns) if (n.equals(x)) return true; return false; }
100
+ String simple() { return n.substring(n.lastIndexOf('.') + 1).replace('$', '.'); }
101
+ @Override public String toString() { return n + (a.isEmpty() ? "" : "<" + String.join(",", a.stream().map(Object::toString).toList()) + ">") + (nul ? "?" : ""); }
102
+ }
103
+ static final Set<String> PRIM = Set.of("int", "long", "short", "byte", "char", "boolean", "float", "double", "void");
104
+ static final Map<String, String> BOX = Map.of("java.lang.Integer", "int", "java.lang.Long", "long", "java.lang.Short", "short", "java.lang.Byte", "byte", "java.lang.Character", "char", "java.lang.Boolean", "boolean", "java.lang.Float", "float", "java.lang.Double", "double");
105
+ static final Set<String> MAPS = Set.of("java.util.Map", "java.util.HashMap", "java.util.LinkedHashMap", "java.util.TreeMap", "java.util.SortedMap", "java.util.NavigableMap", "java.util.concurrent.ConcurrentMap", "java.util.concurrent.ConcurrentHashMap", "java.util.concurrent.ConcurrentSkipListMap", "java.util.Hashtable");
106
+ static final Set<String> LOCKS = Set.of("java.util.concurrent.locks.ReentrantLock", "java.util.concurrent.locks.Lock", "java.util.concurrent.locks.ReentrantReadWriteLock", "java.util.concurrent.locks.ReadWriteLock", "java.util.concurrent.locks.StampedLock", "kotlinx.coroutines.sync.Mutex");
107
+ static final List<String> UNITS = List.of("NANOSECONDS", "MICROSECONDS", "MILLISECONDS", "SECONDS", "MINUTES", "HOURS", "DAYS");
108
+ static final long[] NANOS = {1L, 1_000L, 1_000_000L, 1_000_000_000L, 60_000_000_000L, 3_600_000_000_000L, 86_400_000_000_000L};
109
+ static String prim(JT t) { return PRIM.contains(t.n) ? t.n : BOX.get(t.n); }
110
+ static boolean num(JT t) { String q = prim(t); return q != null && !q.equals("boolean") && !q.equals("void"); }
111
+ static boolean integral(JT t) { String q = prim(t); return q != null && Set.of("int", "long", "short", "byte", "char").contains(q); }
112
+ static boolean floating(JT t) { String q = prim(t); return "float".equals(q) || "double".equals(q); }
113
+ static boolean map(JT t) { return t != null && MAPS.contains(t.n); }
114
+ static boolean atomic(JT t) { return t != null && t.n.startsWith("java.util.concurrent.atomic.Atomic") || t.n.startsWith("kotlinx.atomicfu.Atomic"); }
115
+ // The type an atomic holds.
116
+ static JT held(JT t) {
117
+ return switch (t.simple()) { case "AtomicInteger", "AtomicInt" -> new JT("int"); case "AtomicLong" -> new JT("long"); case "AtomicBoolean" -> new JT("boolean"); default -> t.arg(0); };
118
+ }
119
+ // An integer type's width, sign and name (the executor's IntTy); Kotlin's names where the reader is Kotlin's.
120
+ static boolean kotlin = false;
121
+ static Map<String, Object> ity(JT t) {
122
+ String q = t == null ? null : prim(t);
123
+ if (q == null) return null;
124
+ return switch (q) {
125
+ case "int" -> m("bits", 32, "signed", true, "name", kotlin ? "Int" : "int");
126
+ case "long" -> m("bits", 64, "signed", true, "name", kotlin ? "Long" : "long");
127
+ case "short" -> m("bits", 16, "signed", true, "name", kotlin ? "Short" : "short");
128
+ case "byte" -> m("bits", 8, "signed", true, "name", kotlin ? "Byte" : "byte");
129
+ case "char" -> m("bits", 16, "signed", false, "name", kotlin ? "Char" : "char");
130
+ default -> null;
131
+ };
132
+ }
133
+ // Binary numeric promotion: the type two operands are computed at.
134
+ static JT promote(JT a, JT b) {
135
+ if (floating(a) || floating(b)) return new JT("double");
136
+ if ("long".equals(prim(a)) || "long".equals(prim(b))) return new JT("long");
137
+ return new JT("int");
138
+ }
139
+
140
+ // An argument's type: a float's whole values are exact below 2^24 (a double's below 2^53, the rule's own bound), so a
141
+ // float argument is one of those (the harness refuses to give one past them, as the rule does).
142
+ static Map<String, Object> pstype(JT t, Env env) {
143
+ if (t != null && "float".equals(prim(t))) { Map<String, Object> f = m("k", "int", "bits", 25, "signed", true); return t.nul ? m("k", "opt", "of", f) : f; }
144
+ return stype(t, env);
145
+ }
146
+ // What a reader knows of the code's own types.
147
+ // (other: another module's class or an interface, a record of other code the boundary's inference reads: its
148
+ // methods' declared results, and whether it is a protocol; null where the reader knows nothing of it)
149
+ interface Env { List<String> enumValues(String fq); String rec(String fq); default String other(String fq) { return null; } }
150
+ static Map<String, Object> stype(JT t, Env env) {
151
+ if (t == null) return m("k", "opaque", "why", "a value of no type the reader resolved");
152
+ if (t.nul && !t.is("java.util.Optional")) return m("k", "opt", "of", stype(t.nullable(false), env));
153
+ String q = prim(t);
154
+ if (q != null) {
155
+ Map<String, Object> it = ity(t);
156
+ if (it != null) return m("k", "int", "bits", it.get("bits"), "signed", it.get("signed"));
157
+ return q.equals("boolean") ? m("k", "bool") : q.equals("void") ? m("k", "none") : m("k", "float");
158
+ }
159
+ if (t.is("java.lang.String", "java.lang.CharSequence")) return m("k", "str");
160
+ if (t.is("java.util.Optional")) return m("k", "opt", "of", stype(t.arg(0), env));
161
+ if (t.is("java.util.OptionalInt")) return m("k", "opt", "of", stype(new JT("int"), env));
162
+ if (t.is("java.util.OptionalLong")) return m("k", "opt", "of", stype(new JT("long"), env));
163
+ if (t.is("java.util.concurrent.TimeUnit")) return m("k", "choice", "values", UNITS, "enum", t.n);
164
+ List<String> ev = env.enumValues(t.n);
165
+ if (ev != null) return m("k", "choice", "values", ev, "enum", t.n);
166
+ if (map(t)) return m("k", "map", "key", stype(t.arg(0), env), "val", stype(t.arg(1), env));
167
+ if (atomic(t)) { Map<String, Object> h = new LinkedHashMap<>(stype(held(t), env)); h.put("atomic", t.simple()); return h; }
168
+ if (LOCKS.contains(t.n)) return m("k", "lock");
169
+ String r = env.rec(t.n);
170
+ if (r != null) return m("k", "rec", "name", r);
171
+ if (t.n.startsWith("java.util.") && (t.n.endsWith("List") || t.n.endsWith("Set") || t.n.endsWith("Queue") || t.n.endsWith("Deque") || t.n.equals("java.util.Collection"))) return m("k", "opaque", "why", "a " + t.simple() + " (collections but maps are outside the subset)");
172
+ String o = t.n.startsWith("java.lang.") || t.n.startsWith("kotlin.jvm.functions.") || t.n.startsWith("typevar:") || t.n.startsWith("array:") ? null : env.other(t.n);
173
+ if (o != null) return m("k", "rec", "name", o);
174
+ return m("k", "opaque", "why", "a " + t.simple() + " (a type of other code)");
175
+ }
176
+
177
+ // --- the library, lowered (a call the reader resolved to its declaring type) -----------------------------------------
178
+ // A call: its declaring type and name, the receiver (null for a static one) with its type, the arguments with theirs,
179
+ // the result's type, where it is and its text, and whether its value is used (a statement's isn't). The reader gives
180
+ // a `pure` test (an argument read twice must read the same: no call in it).
181
+ record Call(String owner, String name, Map<String, Object> recv, JT recvT, List<Map<String, Object>> args, List<JT> argT, JT out, String p, String text, boolean stmt, boolean pure) {}
182
+ // Lowered: an expression (`x`), or statements (`ss`) for a call used as one; null where it isn't the library's.
183
+ record Low(Map<String, Object> x, List<Object> ss) {}
184
+ static Low ex(Map<String, Object> x) { return new Low(x, null); }
185
+ static Low st(Object... ss) { return new Low(null, List.of(ss)); }
186
+
187
+ // An operand at a type (Java's binary promotion made explicit: the executor computes at its operands' type).
188
+ static Map<String, Object> at(Map<String, Object> v, JT from, JT to) {
189
+ Map<String, Object> it = ity(to);
190
+ if (it == null || (from != null && Objects.equals(prim(from), prim(to)))) return v;
191
+ if ("int".equals(v.get("e"))) { Map<String, Object> c = new LinkedHashMap<>(v); c.put("ity", it); return c; }
192
+ return x("as", (String) v.get("p"), "x", v, "ity", it);
193
+ }
194
+ static Map<String, Object> arith(String op, Map<String, Object> a, JT ta, Map<String, Object> b, JT tb, JT out, String ovf, String p, String text) {
195
+ if (floating(out)) return x("bin", p, "op", op.equals("/") ? "//" : op, "x", a, "y", b, "text", text, "float", true);
196
+ return x("bin", p, "op", op, "x", at(a, ta, out), "y", at(b, tb, out), "ovf", ovf, "text", text);
197
+ }
198
+ static Low lib(Call c) {
199
+ String o = c.owner, n = c.name, p = c.p, t = c.text;
200
+ List<Map<String, Object>> a = c.args;
201
+ int na = a.size();
202
+ JT a0 = na > 0 ? c.argT.get(0) : null;
203
+ // Math: min and max at the result's type, abs wrapped as Java's (abs of MIN_VALUE is MIN_VALUE), exact arithmetic
204
+ // trapping (an ArithmeticException), floorDiv and floorMod by a constant.
205
+ if (o.equals("java.lang.Math") || o.equals("java.lang.StrictMath") || o.equals("kotlin.math.MathKt")) {
206
+ switch (n) {
207
+ case "min", "max", "minOf", "maxOf": if (na == 2) return ex(x("minmax", p, "op", n.startsWith("min") ? "min" : "max", "xs", List.of(at(a.get(0), a0, c.out), at(a.get(1), c.argT.get(1), c.out)), "ity", ity(c.out), "why", t + ": min or max of no integer type", "text", t)); break;
208
+ case "abs": if (na == 1 && ity(c.out) != null) { Map<String, Object> v = at(a.get(0), a0, c.out); return ex(x("ite", p, "c", cmp("<", v, lit(0, c.out, p), p, t), "a", x("unop", p, "op", "-", "x", v), "b", v)); } break;
209
+ case "addExact", "subtractExact", "multiplyExact": if (na == 2) return ex(arith(n.startsWith("add") ? "+" : n.startsWith("sub") ? "-" : "*", a.get(0), a0, a.get(1), c.argT.get(1), c.out, "trap", p, t)); break;
210
+ case "incrementExact", "decrementExact": if (na == 1) return ex(arith(n.startsWith("inc") ? "+" : "-", a.get(0), a0, lit(1, c.out, p), c.out, c.out, "trap", p, t)); break;
211
+ case "negateExact": if (na == 1) return ex(arith("-", lit(0, c.out, p), c.out, a.get(0), a0, c.out, "trap", p, t)); break;
212
+ case "toIntExact": if (na == 1) return ex(arith("+", x("as", p, "x", a.get(0), "ity", ity(new JT("int"))), c.out, lit(0, c.out, p), c.out, c.out, "trap", p, t)); break;
213
+ case "floorDiv", "floorMod": if (na == 2) return ex(x("as", p, "x", x("bin", p, "op", n.equals("floorDiv") ? "//" : "%", "x", a.get(0), "y", a.get(1), "text", t), "ity", ity(c.out))); break;
214
+ case "clamp": if (na == 3) return ex(x("minmax", p, "op", "max", "xs", List.of(at(a.get(1), c.argT.get(1), c.out), x("minmax", p, "op", "min", "xs", List.of(at(a.get(0), a0, c.out), at(a.get(2), c.argT.get(2), c.out)), "ity", ity(c.out))), "ity", ity(c.out), "text", t)); break;
215
+ }
216
+ return null;
217
+ }
218
+ // Boxing and unboxing are the value; Objects.equals is ==.
219
+ if ((BOX.containsKey(o) || o.equals("java.lang.Number")) && (n.endsWith("Value") && na == 0 && c.recv != null)) return ex(at(c.recv, c.recvT, c.out));
220
+ if (BOX.containsKey(o) && n.equals("valueOf") && na == 1 && !"java.lang.String".equals(a0.n)) return ex(at(a.get(0), a0, c.out));
221
+ if ((o.equals("java.util.Objects") && n.equals("equals") && na == 2)) return ex(cmp("==", a.get(0), a.get(1), p, t));
222
+ if (o.equals("java.lang.String") && c.recv != null) {
223
+ if (n.equals("equals") && na == 1) return ex(cmp("==", c.recv, a.get(0), p, t));
224
+ if (n.equals("isEmpty") && na == 0) return ex(cmp("==", c.recv, str("", p), p, t));
225
+ }
226
+ if (o.equals("java.lang.Enum") && n.equals("ordinal") && na == 0) return ex(x("cast", p, "x", c.recv, "ty", m("k", "int", "bits", 32, "signed", true), "text", t));
227
+ // Checks that throw: Objects.requireNonNull, Guava's Preconditions, Kotlin's check, require, error and their NotNull.
228
+ if (o.equals("java.util.Objects") && n.equals("requireNonNull") && na >= 1) return ex(x("expect", p, "x", a.get(0), "text", t + " (a NullPointerException)"));
229
+ if (o.equals("com.google.common.base.Preconditions") && (n.equals("checkArgument") || n.equals("checkState")) && na >= 1 && c.stmt)
230
+ return st(s("if", p, "c", not(a.get(0), p), "then", List.of(fail(p, t + " (an Illegal" + (n.equals("checkState") ? "State" : "Argument") + "Exception)")), "else", List.of()));
231
+ if (o.equals("com.google.common.base.Preconditions") && n.equals("checkNotNull") && na >= 1) return ex(x("expect", p, "x", a.get(0), "text", t + " (a NullPointerException)"));
232
+ if (o.startsWith("kotlin.") && (n.equals("check") || n.equals("require")) && na >= 1 && c.stmt)
233
+ return st(s("if", p, "c", not(a.get(0), p), "then", List.of(fail(p, t + " failed (an Illegal" + (n.equals("check") ? "State" : "Argument") + "Exception)")), "else", List.of()));
234
+ if (o.startsWith("kotlin.") && (n.equals("checkNotNull") || n.equals("requireNotNull")) && na >= 1) return ex(x("expect", p, "x", a.get(0), "text", t + " (an Illegal" + (n.equals("checkNotNull") ? "State" : "Argument") + "Exception)"));
235
+ if (o.startsWith("kotlin.") && n.equals("error") && na == 1 && c.stmt) return st(fail(p, t + " (an IllegalStateException)"));
236
+ // The clock: the request's time (the harness's, through the check's rewrite of the call).
237
+ if ((o.equals("java.lang.System") && (n.equals("currentTimeMillis") || n.equals("nanoTime"))) || (o.equals("java.time.Clock") && n.equals("millis")) || (o.equals("java.time.InstantSource") && n.equals("millis"))
238
+ || (o.startsWith("kotlin.system.") && (n.equals("getTimeMillis") || n.equals("currentTimeMillis"))))
239
+ return ex(x("clock", p, "text", t));
240
+ // TimeUnit's conversions: the unit's factor, truncated (Java's toX truncates toward zero; its saturation at
241
+ // Long's range is past the rule's ±2^53).
242
+ if (o.equals("java.util.concurrent.TimeUnit") && c.recv != null && n.startsWith("to") && na == 1 && !n.equals("toChronoUnit") && c.pure) {
243
+ int to = switch (n) { case "toNanos" -> 0; case "toMicros" -> 1; case "toMillis" -> 2; case "toSeconds" -> 3; case "toMinutes" -> 4; case "toHours" -> 5; case "toDays" -> 6; default -> -1; };
244
+ if (to < 0) return null;
245
+ JT lt = new JT("long");
246
+ Map<String, Object> d = at(a.get(0), a0, lt), acc = null;
247
+ for (int u = UNITS.size() - 1; u >= 0; u--) {
248
+ Map<String, Object> v = u == to ? d : u > to ? x("bin", p, "op", "*", "x", d, "y", lit(NANOS[u] / NANOS[to], lt, p), "ovf", "sat", "text", t) : x("bin", p, "op", "/", "x", d, "y", lit(NANOS[to] / NANOS[u], lt, p), "ovf", "wrap", "text", t);
249
+ acc = acc == null ? v : x("ite", p, "c", cmp("==", c.recv, str(UNITS.get(u), p), p, t), "a", v, "b", acc);
250
+ }
251
+ return ex(acc);
252
+ }
253
+ // Optional: empty is None, of is Some, isPresent and isEmpty test it, get and orElseThrow unwrap it (a failure where
254
+ // it is empty: NoSuchElementException), orElse gives a default.
255
+ if (o.equals("java.util.Optional") || o.equals("java.util.OptionalInt") || o.equals("java.util.OptionalLong")) {
256
+ switch (n) {
257
+ case "empty": return ex(x("none", p));
258
+ case "of": return ex(x("some", p, "x", a.get(0)));
259
+ case "ofNullable": return ex(a.get(0));
260
+ case "isPresent": return ex(x("is", p, "x", c.recv, "k", "some", "text", t));
261
+ case "isEmpty": return ex(x("is", p, "x", c.recv, "k", "none", "text", t));
262
+ case "get", "getAsInt", "getAsLong", "orElseThrow": if (na == 0) return ex(x("expect", p, "x", c.recv, "text", t + " (a NoSuchElementException)")); break;
263
+ case "orElse": return ex(x("opt_or", p, "x", c.recv, "d", a.get(0)));
264
+ }
265
+ return null;
266
+ }
267
+ // Maps: get (None where the key isn't held), getOrDefault, containsKey, put and remove (their old values), a put
268
+ // as a statement one key written.
269
+ if (MAPS.contains(o) || o.equals("kotlin.collections.MapsKt") || o.startsWith("kotlin.collections.Map") || o.startsWith("kotlin.collections.MutableMap")) {
270
+ Map<String, Object> mm = c.recv != null ? c.recv : na > 0 ? a.get(0) : null;
271
+ List<Map<String, Object>> rest = c.recv != null ? a : a.subList(1, na);
272
+ if (mm == null) return null;
273
+ Map<String, Object> getf = get(mm, "get", p, t);
274
+ switch (n) {
275
+ case "get": if (rest.size() == 1) return ex(call(getf, rest, p, t)); break;
276
+ case "getOrDefault": if (rest.size() == 2) return ex(call(getf, rest, p, t)); break;
277
+ case "containsKey": if (rest.size() == 1) return ex(cmp("in", rest.get(0), mm, p, t)); break;
278
+ case "getValue": if (rest.size() == 1) return ex(x("idx", p, "x", mm, "i", rest.get(0), "text", t + " (a NoSuchElementException where the key isn't held)")); break;
279
+ case "put", "set": if (rest.size() == 2) return c.stmt ? st(s("set", p, "to", x("idx", p, "x", mm, "i", rest.get(0), "text", t), "x", rest.get(1))) : ex(call(get(mm, "insert", p, t), rest, p, t)); break;
280
+ case "remove": if (rest.size() == 1) return ex(call(get(mm, "remove", p, t), rest, p, t)); break;
281
+ }
282
+ return ex(no(p, t + ": " + n + " of a map (the subset reads get, getOrDefault, containsKey, put and remove)"));
283
+ }
284
+ // The atomics: a read, a write, a read-modify-write (each the field it is: one request one step).
285
+ if (atomic(c.recvT) && c.recv != null) {
286
+ Map<String, Object> pl = c.recv;
287
+ JT h = held(c.recvT);
288
+ Map<String, Object> one = lit(1, h, p);
289
+ switch (n) {
290
+ case "get", "getPlain", "getAcquire", "getOpaque", "intValue", "longValue", "getValue": if (na == 0) return ex(pl); break;
291
+ case "set", "lazySet", "setPlain", "setRelease", "setOpaque", "setValue": if (na == 1) return c.stmt ? st(s("set", p, "to", pl, "x", at(a.get(0), a0, h))) : ex(x("rmw", p, "op", "store", "place", pl, "args", List.of(a.get(0)), "text", t)); break;
292
+ case "getAndSet": if (na == 1) return ex(x("rmw", p, "op", "swap", "place", pl, "args", List.of(at(a.get(0), a0, h)), "text", t)); break;
293
+ case "getAndIncrement", "getAndDecrement": if (na == 0) return ex(x("rmw", p, "op", n.endsWith("Increment") ? "fetch_add" : "fetch_sub", "place", pl, "args", List.of(one), "text", t)); break;
294
+ case "incrementAndGet", "decrementAndGet": if (na == 0) return ex(arith(n.startsWith("inc") ? "+" : "-", x("rmw", p, "op", n.startsWith("inc") ? "fetch_add" : "fetch_sub", "place", pl, "args", List.of(one), "text", t), h, one, h, h, "wrap", p, t)); break;
295
+ case "getAndAdd": if (na == 1) return ex(x("rmw", p, "op", "fetch_add", "place", pl, "args", List.of(at(a.get(0), a0, h)), "text", t)); break;
296
+ case "addAndGet": if (na == 1 && c.pure) return ex(arith("+", x("rmw", p, "op", "fetch_add", "place", pl, "args", List.of(at(a.get(0), a0, h)), "text", t), h, a.get(0), a0, h, "wrap", p, t)); break;
297
+ case "compareAndSet", "weakCompareAndSet", "weakCompareAndSetPlain", "weakCompareAndSetVolatile", "compareAndExchange":
298
+ if (na == 2) { Map<String, Object> r = x("rmw", p, "op", "compare_exchange", "place", pl, "args", List.of(at(a.get(0), a0, h), at(a.get(1), c.argT.get(1), h)), "text", t); return ex(n.equals("compareAndExchange") ? x("unwrap", p, "x", r, "k", "ok") : x("is", p, "x", r, "k", "ok", "text", t)); }
299
+ break;
300
+ }
301
+ return ex(no(p, t + ": " + c.recvT.simple() + "." + n + " (the subset reads get, set, getAndSet, the increments and adds, compareAndSet)"));
302
+ }
303
+ return null;
304
+ }
305
+
306
+ // --- what the caller's boundary inference reads ----------------------------------------------------------------------
307
+ // A declaration's annotations (DI ones among them) and its `// gdp:` tags, as the reader saw them.
308
+ // An injection by the framework (a field or a constructor's argument the boundary's inference holds as a store).
309
+ static final Set<String> INJECT = Set.of("Autowired", "Inject", "Resource");
310
+ // The boundary's tags as annotations (@GdpSkip, @GdpAnswer("int"), @GdpStrict): a function's decorators.
311
+ static boolean gdpTag(String name) { return name.matches("(?i)gdp[._]?(skip|answer|strict)"); }
312
+ static final Set<String> DI = Set.of("Autowired", "Inject", "Resource", "Value", "Qualifier", "Named", "Lazy", "MockBean", "Bean", "Component", "Service", "Repository", "Configuration");
313
+ static Map<String, Object> anno(String name, String fq, String text) { return m("name", name, "fq", fq, "text", text, "di", DI.contains(name) ? true : null); }
314
+ // `// gdp: ...` comments of a file, by the line they are on (a tag names the declaration or call on its line, or on
315
+ // the next line that has code).
316
+ static TreeMap<Integer, String> tags(String src) {
317
+ TreeMap<Integer, String> out = new TreeMap<>();
318
+ String[] lines = src.split("\n", -1);
319
+ for (int i = 0; i < lines.length; i++) {
320
+ int k = lines[i].indexOf("gdp:");
321
+ if (k < 0) continue;
322
+ String before = lines[i].substring(0, k).trim();
323
+ if (!(before.endsWith("//") || before.endsWith("/*") || before.endsWith("*"))) continue;
324
+ String t = lines[i].substring(k + 4).replace("*/", "").trim();
325
+ int at = i + 1;
326
+ String code = lines[i].substring(0, lines[i].indexOf(before.endsWith("//") ? "//" : before.endsWith("/*") ? "/*" : "*")).trim();
327
+ if (code.isEmpty()) { int j = i + 1; while (j < lines.length && (lines[j].trim().isEmpty() || lines[j].trim().startsWith("//") || lines[j].trim().startsWith("@"))) j++; at = j + 1; }
328
+ out.merge(at, t, (x, y) -> x + "; " + y);
329
+ }
330
+ return out;
331
+ }
332
+ }