gdp-wired 0.44.0 → 0.46.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 +58 -7
- package/dist/core/bitsets.d.ts +2 -0
- package/dist/core/bitsets.js +23 -0
- package/dist/core/bmc.js +6 -2
- package/dist/core/c/reader.d.ts +9 -1
- package/dist/core/c/reader.js +445 -38
- package/dist/core/calendar.d.ts +29 -0
- package/dist/core/calendar.js +82 -0
- package/dist/core/check.js +3 -2
- package/dist/core/cli.d.ts +4 -0
- package/dist/core/cli.js +335 -39
- package/dist/core/compose.js +3 -1
- package/dist/core/counts.d.ts +5 -2
- package/dist/core/counts.js +335 -88
- package/dist/core/cycles.js +8 -1
- package/dist/core/cyclic.d.ts +1 -0
- package/dist/core/cyclic.js +13 -7
- package/dist/core/data-synth.d.ts +3 -0
- package/dist/core/data-synth.js +252 -0
- package/dist/core/data.d.ts +8 -2
- package/dist/core/data.js +1113 -172
- package/dist/core/discover.js +3 -1
- package/dist/core/draft.d.ts +19 -0
- package/dist/core/draft.js +53 -0
- package/dist/core/dsl.d.ts +39 -3
- package/dist/core/dsl.js +250 -31
- package/dist/core/effect-prims.js +2 -2
- package/dist/core/effect.d.ts +1 -4
- package/dist/core/effect.js +1 -15
- package/dist/core/ex/reader.exs +17 -1
- package/dist/core/exists.d.ts +16 -1
- package/dist/core/exists.js +447 -50
- package/dist/core/extract-c.d.ts +2 -0
- package/dist/core/extract-c.js +185 -31
- package/dist/core/extract-check.d.ts +7 -0
- package/dist/core/extract-check.js +115 -3
- package/dist/core/extract-ex.js +3 -3
- package/dist/core/extract-go-check.d.ts +1 -1
- package/dist/core/extract-go-check.js +21 -13
- package/dist/core/extract-go.d.ts +1 -0
- package/dist/core/extract-go.js +58 -9
- package/dist/core/extract-ir.d.ts +1 -0
- package/dist/core/extract-ir.js +15 -0
- package/dist/core/extract-jvm.d.ts +1 -1
- package/dist/core/extract-jvm.js +65 -14
- package/dist/core/extract-orm.d.ts +12 -0
- package/dist/core/extract-orm.js +244 -0
- package/dist/core/extract-print.js +23 -1
- package/dist/core/extract-py.d.ts +1 -0
- package/dist/core/extract-py.js +4 -4
- package/dist/core/extract-rb.js +3 -3
- package/dist/core/extract-read.d.ts +25 -0
- package/dist/core/extract-read.js +60 -5
- package/dist/core/extract-rs.js +22 -20
- package/dist/core/extract-shapes.js +22 -0
- package/dist/core/extract-sol.d.ts +10 -1
- package/dist/core/extract-sol.js +89 -30
- package/dist/core/extract-step.d.ts +201 -4
- package/dist/core/extract-step.js +1514 -111
- package/dist/core/extract-swift.js +24 -13
- package/dist/core/extract.d.ts +9 -0
- package/dist/core/extract.js +88 -7
- package/dist/core/fiber-run.d.ts +5 -0
- package/dist/core/fiber-run.js +18 -0
- package/dist/core/fresh.d.ts +2 -2
- package/dist/core/fresh.js +7 -3
- package/dist/core/go/reader.go +7 -1
- package/dist/core/infer.d.ts +4 -1
- package/dist/core/infer.js +1020 -246
- package/dist/core/jvm/JavaReader.java +33 -8
- package/dist/core/jvm/Jvm.java +20 -1
- package/dist/core/jvm/KtReader.java +59 -6
- package/dist/core/laws.d.ts +10 -3
- package/dist/core/laws.js +193 -14
- package/dist/core/machine.d.ts +16 -3
- package/dist/core/machine.js +212 -28
- package/dist/core/nonterm.js +14 -1
- package/dist/core/orm/harness.js +61 -0
- package/dist/core/orm-fixtures/schema.prisma +5 -0
- package/dist/core/py/harness.py +260 -21
- package/dist/core/py/reader.py +278 -39
- package/dist/core/rb/reader.rb +52 -5
- package/dist/core/reader-fixtures/README.md +28 -0
- package/dist/core/reader-fixtures/c-693042.cpp +8 -0
- package/dist/core/reader-fixtures/go-685506.go +14 -0
- package/dist/core/reader-fixtures/jvm-724362.kt +8 -0
- package/dist/core/reader-fixtures/jvm-737430.kt +7 -0
- package/dist/core/reader-fixtures/jvm-765282.kt +3 -0
- package/dist/core/reader-fixtures/swift-735726.swift +15 -0
- package/dist/core/reader-fixtures/swift-735990.swift +15 -0
- package/dist/core/reader-fixtures/swift-739158.swift +4 -0
- package/dist/core/reader-fixtures/swift-744834.swift +15 -0
- package/dist/core/reader-fixtures/swift-746946.swift +31 -0
- package/dist/core/reader-fixtures/typescript-673062.ts.txt +2 -0
- package/dist/core/relation.d.ts +1 -1
- package/dist/core/relation.js +29 -2
- package/dist/core/relational.js +4 -2
- package/dist/core/rs/src/main.rs +18 -2
- package/dist/core/sample-cache.d.ts +8 -0
- package/dist/core/sample-cache.js +46 -0
- package/dist/core/security.d.ts +38 -0
- package/dist/core/security.js +88 -0
- package/dist/core/sol/harness.py +63 -7
- package/dist/core/sol-concrete.d.ts +23 -0
- package/dist/core/sol-concrete.js +195 -0
- package/dist/core/solver.js +8 -5
- package/dist/core/swift/Sources/gdp-swiftreader/main.swift +50 -7
- package/dist/core/system.d.ts +62 -1
- package/dist/core/system.js +263 -4
- package/dist/core/transfer.js +39 -9
- package/dist/core/translate.js +39 -10
- package/dist/core/watch.js +51 -6
- package/dist/interchange/check.js +10 -10
- package/dist/interchange/format.d.ts +4 -0
- package/dist/interchange/format.js +24 -6
- package/dist/kernel/cdcl.d.ts +5 -0
- package/dist/kernel/cdcl.js +88 -10
- package/dist/kernel/epr.js +141 -29
- package/dist/kernel/kernel.d.ts +31 -1
- package/dist/kernel/kernel.js +391 -101
- package/dist/kernel/model-sequence.d.ts +3 -0
- package/dist/kernel/model-sequence.js +34 -0
- package/dist/kernel/prove.d.ts +30 -1
- package/dist/kernel/prove.js +481 -71
- package/dist/kernel/reader.d.ts +3 -0
- package/dist/kernel/reader.js +101 -14
- package/dist/kernel/share.d.ts +1 -0
- package/dist/kernel/share.js +27 -0
- package/dist/lean-kernel/emit.d.ts +9 -3
- package/dist/lean-kernel/emit.js +200 -66
- package/dist/lean-kernel/lean-terms.js +5 -0
- package/dist/lean-kernel/library.js +10 -10
- package/package.json +7 -2
package/dist/core/c/reader.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Refusal } from "../extract-read.js";
|
|
1
|
+
import { Refusal, readerRun, statedRefusal } from "../extract-read.js";
|
|
2
2
|
// (consecutive JSON values in one string, each with where it ends)
|
|
3
3
|
class JSONDecoder {
|
|
4
4
|
decode(s, i) {
|
|
@@ -31,10 +31,13 @@ export function clangDump(file, flags, clang = 'clang', cpp = false, filter) {
|
|
|
31
31
|
const cp = node('node:child_process'), lang = cpp ? ['-x', 'c++'] : ['-x', 'c'];
|
|
32
32
|
const run = (args) => {
|
|
33
33
|
try {
|
|
34
|
-
return cp.execFileSync(clang, args, { encoding: 'utf8', maxBuffer: 1 << 30, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
34
|
+
return cp.execFileSync(clang, args, { encoding: 'utf8', maxBuffer: 1 << 30, stdio: ['ignore', 'pipe', 'pipe'], ...readerRun() });
|
|
35
35
|
}
|
|
36
36
|
catch (e) {
|
|
37
37
|
const x = e;
|
|
38
|
+
const stated = statedRefusal(e);
|
|
39
|
+
if (stated)
|
|
40
|
+
throw new Refusal(file, stated);
|
|
38
41
|
throw new Refusal(file, x.code === 'ENOENT' ? `no ${clang} to run: clang is needed to extract ${cpp ? 'C++' : 'C'}` : `clang doesn't compile it (${String(x.stderr ?? x.message).split('\n').filter(l => /error/.test(l)).slice(0, 3).join(' | ')})`);
|
|
39
42
|
}
|
|
40
43
|
};
|
|
@@ -49,6 +52,46 @@ export function clangDump(file, flags, clang = 'clang', cpp = false, filter) {
|
|
|
49
52
|
const defs = run([...lang, ...flags, '-dM', '-E', '/dev/null']), def = (k) => Number(new RegExp(`#define ${k} (\\d+)`).exec(defs)?.[1] ?? 0);
|
|
50
53
|
return { tu, target: { int: def('__SIZEOF_INT__') * 8, long: def('__SIZEOF_LONG__') * 8, short: def('__SIZEOF_SHORT__') * 8, llong: def('__SIZEOF_LONG_LONG__') * 8, charUnsigned: /#define __CHAR_UNSIGNED__/.test(defs) } };
|
|
51
54
|
}
|
|
55
|
+
// The values of names a law reads that the code defines as integer constants (a macro, an enum constant, a constexpr
|
|
56
|
+
// variable), folded by clang itself: each the value of an enumerator of a probe that includes the file. A name that
|
|
57
|
+
// isn't one (a field, a float, a string) doesn't compile there and is left out.
|
|
58
|
+
export function constantsOf(file, flags, clang, cpp, names) {
|
|
59
|
+
const fs = node('node:fs'), os = node('node:os'), path = node('node:path'), cp = node('node:child_process');
|
|
60
|
+
let left = [...new Set(names)].filter(n => /^[A-Za-z_]\w*(::[A-Za-z_]\w*)*$/.test(n) && (cpp || !n.includes('::')));
|
|
61
|
+
if (!left.length)
|
|
62
|
+
return {};
|
|
63
|
+
const work = fs.mkdtempSync(`${os.tmpdir()}/gdp-cconst-`), probe = `${work}/probe.${cpp ? 'cpp' : 'c'}`, out = {};
|
|
64
|
+
try {
|
|
65
|
+
for (let round = 0; round < 3 && left.length; round++) {
|
|
66
|
+
fs.writeFileSync(probe, `#include ${JSON.stringify(path.resolve(file))}\n${left.map((n, i) => `enum gdp_k${i}_e ${cpp ? ': long long ' : ''}{ gdp_k${i} = (${n}) };`).join('\n')}\n`);
|
|
67
|
+
let dump = '';
|
|
68
|
+
try {
|
|
69
|
+
dump = cp.execFileSync(clang, [...(cpp ? ['-x', 'c++'] : ['-x', 'c']), ...flags, '-w', '-ferror-limit=0', '-Xclang', '-ast-dump=json', '-Xclang', '-ast-dump-filter=gdp_k', '-fsyntax-only', probe], { encoding: 'utf8', maxBuffer: 1 << 28, stdio: ['ignore', 'pipe', 'pipe'], ...readerRun() });
|
|
70
|
+
}
|
|
71
|
+
catch (e) {
|
|
72
|
+
// (the lines that don't compile: their names left out, the rest tried again)
|
|
73
|
+
const bad = new Set([...String(e.stderr ?? '').matchAll(/probe\.c(?:pp)?:(\d+):\d+: error/g)].map(m => Number(m[1]) - 2));
|
|
74
|
+
if (!bad.size)
|
|
75
|
+
return out;
|
|
76
|
+
left = left.filter((_, i) => !bad.has(i));
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
for (const m of dump.matchAll(/"kind":\s*"EnumConstantDecl"[^]*?"name":\s*"gdp_k(\d+)"[^]*?"value":\s*"(-?\d+)"/g)) {
|
|
80
|
+
const v = Number(m[2]);
|
|
81
|
+
if (Number.isSafeInteger(v))
|
|
82
|
+
out[left[Number(m[1])]] = v;
|
|
83
|
+
}
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
88
|
+
finally {
|
|
89
|
+
try {
|
|
90
|
+
fs.rmSync(work, { recursive: true, force: true });
|
|
91
|
+
}
|
|
92
|
+
catch { /* gone */ }
|
|
93
|
+
}
|
|
94
|
+
}
|
|
52
95
|
// The project's flags for a file: from a compile_commands.json (its -I, -D, -std, -include and -f options), or given.
|
|
53
96
|
export function flagsFor(file, compileCommands) {
|
|
54
97
|
if (!compileCommands)
|
|
@@ -98,9 +141,13 @@ function locate(root) {
|
|
|
98
141
|
n.$b = at(n.range.begin);
|
|
99
142
|
n.$e = at(n.range.end);
|
|
100
143
|
}
|
|
101
|
-
|
|
144
|
+
// (doc comments, FullComment and its parts, are no code)
|
|
145
|
+
// (a for's absent parts are empty objects, kind-less)
|
|
146
|
+
else if (k === 'inner') {
|
|
147
|
+
n.inner = n.inner.filter(c => !(c.kind ?? '').endsWith('Comment'));
|
|
102
148
|
for (const c of n.inner)
|
|
103
149
|
walk(c);
|
|
150
|
+
}
|
|
104
151
|
}
|
|
105
152
|
n.$at ??= n.$b;
|
|
106
153
|
};
|
|
@@ -113,7 +160,9 @@ const FAIL = { abort: 'abort()', __assert_rtn: 'an assertion failing', __assert_
|
|
|
113
160
|
// (`root`: the project's own files, whose functions are read too: a static inline helper of its header; by default the
|
|
114
161
|
// file's directory)
|
|
115
162
|
// (`picks`: an overloaded method's arity, the one read: `functions: ['throttle/1']`)
|
|
116
|
-
|
|
163
|
+
// (leave: globals the rule leaves out, an output the code writes for other code, never read into what it keeps)
|
|
164
|
+
// (cut: the file's own functions a call of which the request answers, as another file's)
|
|
165
|
+
export function readC(file, entry, tu, target, cpp = false, root, picks = {}, pickNames = [], leave = [], cut = []) {
|
|
117
166
|
const path = node('node:path'), fs = node('node:fs');
|
|
118
167
|
const main = path.resolve(file), base = path.basename(file), dir = `${path.resolve(root ?? main.replace(/\/[^/]*$/, ''))}/`, srcs = new Map();
|
|
119
168
|
locate(tu);
|
|
@@ -133,12 +182,19 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
133
182
|
const text = (n) => (n.$b && n.$e && n.$b.file === n.$e.file && own(n) && n.$e.off + n.$e.tok >= n.$b.off ? src(n.$b.file).slice(n.$b.off, n.$e.off + n.$e.tok) || n.kind : n.kind);
|
|
134
183
|
// The declarations: typedefs, records and enums of every file (a header's types are the file's too), the globals.
|
|
135
184
|
const typedefs = new Map(), records = new Map(), tags = new Map(), anon = new Map();
|
|
136
|
-
const members = new Map(), fieldNode = new Map(), enums = new Map(), cxx = [];
|
|
137
|
-
const enumConst = new Map(), globals = new Map(), fields = new Map(), recName = new Map();
|
|
185
|
+
const members = new Map(), fieldNode = new Map(), enums = new Map(), enumById = new Map(), cxx = [];
|
|
186
|
+
const enumConst = new Map(), named = new Map(), globals = new Map(), fields = new Map(), recName = new Map();
|
|
138
187
|
const declOf = (n) => n.decl ?? (n.inner ?? []).map(declOf).find(Boolean);
|
|
139
188
|
const visitDecl = (n) => {
|
|
140
189
|
if (n.kind === 'TypedefDecl' && n.name) {
|
|
141
190
|
const d = declOf(n);
|
|
191
|
+
// (a typedef of an unnamed enum names it: clang 21 spells it `enum state_t`, desugared to the typedef's own name)
|
|
192
|
+
const en = d?.kind === 'EnumDecl' ? enumById.get(d.id) : undefined;
|
|
193
|
+
if (en && !en.name) {
|
|
194
|
+
for (const q of [n.name, `enum ${n.name}`])
|
|
195
|
+
enums.set(q, en);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
142
198
|
typedefs.set(n.name, { qual: n.type.qualType, ...(n.type.desugaredQualType ? { desug: n.type.desugaredQualType } : {}), ...(d && (d.kind === 'RecordDecl' || d.kind === 'CXXRecordDecl') ? { rec: d.id } : {}) });
|
|
143
199
|
if (d && !recName.has(d.id))
|
|
144
200
|
recName.set(d.id, n.name);
|
|
@@ -175,6 +231,7 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
175
231
|
}
|
|
176
232
|
}
|
|
177
233
|
else if (n.kind === 'EnumDecl') {
|
|
234
|
+
enumById.set(n.id, n);
|
|
178
235
|
if (n.name)
|
|
179
236
|
for (const q of [n.name, ...(n.$outer ? [`${n.$outer}::${n.name}`] : []), `enum ${n.name}`])
|
|
180
237
|
enums.set(q, n);
|
|
@@ -184,6 +241,8 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
184
241
|
const k = c.inner?.length ? fold(c.inner[0]) : undefined;
|
|
185
242
|
v = k ?? v + 1;
|
|
186
243
|
enumConst.set(c.id, v);
|
|
244
|
+
if (c.name)
|
|
245
|
+
named.set(c.name, named.has(c.name) && named.get(c.name) !== v ? NaN : v);
|
|
187
246
|
}
|
|
188
247
|
}
|
|
189
248
|
else if (n.kind === 'VarDecl')
|
|
@@ -198,11 +257,17 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
198
257
|
cxx.push(n);
|
|
199
258
|
}
|
|
200
259
|
};
|
|
260
|
+
// A literal's value exactly, through parentheses.
|
|
261
|
+
const exact = (n) => n.kind === 'IntegerLiteral' ? BigInt(String(n.value)) : n.kind === 'ParenExpr' ? exact(n.inner[0]) : undefined;
|
|
201
262
|
// A constant's value, where the node is one (literals, enum constants, a const global's, folded).
|
|
202
263
|
const fold = (n) => {
|
|
203
264
|
switch (n.kind) {
|
|
265
|
+
// (a literal a double can't hold exactly: NaN, past 2^53, unless a cast takes it exactly)
|
|
204
266
|
case 'IntegerLiteral':
|
|
205
|
-
case 'CharacterLiteral':
|
|
267
|
+
case 'CharacterLiteral': {
|
|
268
|
+
const v = Number(n.value);
|
|
269
|
+
return Number.isSafeInteger(v) ? v : NaN;
|
|
270
|
+
}
|
|
206
271
|
case 'CXXBoolLiteralExpr': return n.value ? 1 : 0;
|
|
207
272
|
case 'CallExpr': {
|
|
208
273
|
const m = /numeric_limits<\s*(.+?)\s*>::(max|min|lowest)\)?\s*\(\s*\)$/.exec(text(n)), it = m ? T(parseT(m[1])) : undefined;
|
|
@@ -217,11 +282,13 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
217
282
|
case 'CStyleCastExpr':
|
|
218
283
|
case 'CXXStaticCastExpr':
|
|
219
284
|
case 'CXXFunctionalCastExpr': {
|
|
220
|
-
const v = fold(n.inner[0]), t = n.type ? T(tyOf(n.type)) : undefined;
|
|
285
|
+
const b = exact(n.inner[0]), v = b !== undefined ? Number(b) : fold(n.inner[0]), t = n.type ? T(tyOf(n.type)) : undefined;
|
|
221
286
|
if (v === undefined || t?.k !== 'int')
|
|
222
287
|
return n.castKind === 'IntegralToBoolean' && v !== undefined ? +(v !== 0) : undefined;
|
|
288
|
+
if (b === undefined && !Number.isFinite(v))
|
|
289
|
+
return NaN;
|
|
223
290
|
// (exactly: at 64 bits 2^64 + 1 is 2^64 in a double)
|
|
224
|
-
const m = 1n << BigInt(t.bits), w = ((BigInt(Math.trunc(v)) % m) + m) % m;
|
|
291
|
+
const m = 1n << BigInt(t.bits), w = (((b ?? BigInt(Math.trunc(v))) % m) + m) % m;
|
|
225
292
|
return Number(t.signed && w >= m / 2n ? w - m : w);
|
|
226
293
|
}
|
|
227
294
|
case 'UnaryOperator': {
|
|
@@ -279,7 +346,9 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
279
346
|
if (r.tagUsed === 'union')
|
|
280
347
|
return add(key, { k: 'union', go });
|
|
281
348
|
const t = add(key, { k: 'struct', own: true, name: go, go, fields: [] });
|
|
282
|
-
|
|
349
|
+
// (a void * field: a flag where the code only sets it to NULL or not and tests it against NULL, CANopen's CO_FLAG;
|
|
350
|
+
// any other use of it refused where it is)
|
|
351
|
+
ttab[t].fields = (r.inner ?? []).filter(f => f.kind === 'FieldDecl').map(f => { const ft = tyOf(f.type), pt = T(ft); return { name: f.name ?? '', t: pt.k === 'ptr' && T(pt.elem).k === 'void' ? add(`nullflag:${f.type.qualType}`, { k: 'bool', go: f.type.qualType, nullflag: true }) : ft, embedded: false }; });
|
|
283
352
|
return t;
|
|
284
353
|
};
|
|
285
354
|
function tyOf(ty) { return parseT(ty.qualType, ty.desugaredQualType); }
|
|
@@ -290,6 +359,12 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
290
359
|
const arr = /^(.*?)\s*\[(\d+)\]$/.exec(s);
|
|
291
360
|
if (arr)
|
|
292
361
|
return add(`arr:${s}`, { k: 'array', go: s, elem: parseT(arr[1]), len: Number(arr[2]), fixed: true });
|
|
362
|
+
// (an unnamed struct's spelling, `struct (unnamed struct at f.c:3:8)`, is no function type)
|
|
363
|
+
const un0 = /^(struct|union|class) \((?:unnamed|anonymous) (?:struct|union|class) at .*:(\d+):(\d+)\)$/.exec(s);
|
|
364
|
+
if (un0) {
|
|
365
|
+
const id = anon.get(`${un0[2]}:${un0[3]}`);
|
|
366
|
+
return id ? recT(id) : add(`opaque:${s}`, { k: 'opaque', go: s });
|
|
367
|
+
}
|
|
293
368
|
if (/\(\*\)|\(.*\)$/.test(s))
|
|
294
369
|
return add(`fn:${s}`, { k: 'func', go: s });
|
|
295
370
|
if (/\*$/.test(s)) {
|
|
@@ -342,6 +417,19 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
342
417
|
const enumT = (s) => {
|
|
343
418
|
const d = enums.get(s), vals = (d?.inner ?? []).filter(c => c.kind === 'EnumConstantDecl').map(c => enumConst.get(c.id) ?? 0);
|
|
344
419
|
const fixed = d?.fixedUnderlyingType?.qualType, ft = fixed ? T(parseT(fixed)) : undefined;
|
|
420
|
+
// (C++, no fixed type: its values are those of the smallest bit-field holding every enumerator, another one
|
|
421
|
+
// undefined to make, the sanitizer's trap where it is read)
|
|
422
|
+
if (cpp && !ft) {
|
|
423
|
+
const lo = Math.min(0, ...vals), hi = Math.max(0, ...vals);
|
|
424
|
+
let b = 1;
|
|
425
|
+
if (lo < 0)
|
|
426
|
+
while (-(2 ** (b - 1)) > lo || hi > 2 ** (b - 1) - 1)
|
|
427
|
+
b++;
|
|
428
|
+
else
|
|
429
|
+
while (hi > 2 ** b - 1)
|
|
430
|
+
b++;
|
|
431
|
+
return add(`int:${s}`, { k: 'int', go: s, bits: b, signed: lo < 0, ub: true });
|
|
432
|
+
}
|
|
345
433
|
return intT(s, ft?.bits ?? target.int, ft ? ft.signed : vals.some(v => v < 0));
|
|
346
434
|
};
|
|
347
435
|
for (const n of top)
|
|
@@ -367,9 +455,13 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
367
455
|
for (const m of cxx) {
|
|
368
456
|
if (m.$rec !== entryRec)
|
|
369
457
|
continue;
|
|
458
|
+
// (a constructor the check can't call, private or protected: refused, the object is made with it)
|
|
370
459
|
if (m.kind === 'CXXConstructorDecl') {
|
|
371
|
-
if (!/\((const )?\w+ &&?\)/.test(m.type.qualType) || params(m).length !== 1)
|
|
460
|
+
if (!/\((const )?\w+ &&?\)/.test(m.type.qualType) || params(m).length !== 1) {
|
|
461
|
+
if (m.$access !== 'public')
|
|
462
|
+
refuse(m, `${entry}: its constructor is ${String(m.$access)} (the check makes the object with it)`);
|
|
372
463
|
ctors.push(m);
|
|
464
|
+
}
|
|
373
465
|
continue;
|
|
374
466
|
}
|
|
375
467
|
if (m.kind !== 'CXXMethodDecl' || /^operator/.test(m.name))
|
|
@@ -384,10 +476,71 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
384
476
|
overloaded.add(m.name);
|
|
385
477
|
funcs.set(m.name, m);
|
|
386
478
|
}
|
|
387
|
-
|
|
479
|
+
// The file's globals as the state (C, the entry the file's stem, no struct of that name): each global its functions
|
|
480
|
+
// read or write a field of one object, `this`, every function of the file a method of it. A global the functions
|
|
481
|
+
// only read that the file doesn't own (extern, volatile, or another file's to write: not static) is the request's:
|
|
482
|
+
// the value each request finds there, set by the check before the call (read once a request).
|
|
483
|
+
const stem = base.replace(/\.[^.]*$/, ''), gmode = !entryRec && !cpp && entry === stem;
|
|
484
|
+
const gfield = new Map(), ginput = new Set(), gnodes = [], fnReads = new Map(), left = new Set();
|
|
485
|
+
if (gmode) {
|
|
486
|
+
const reads = new Set(), writes = new Set(), all = new Set();
|
|
487
|
+
const bare = (n) => ((n.kind === 'ParenExpr' || CAST.has(n.kind) && ['LValueToRValue', 'NoOp'].includes(n.castKind)) && n.inner?.length ? bare(n.inner[0]) : n);
|
|
488
|
+
const root = (n0) => { const n = bare(n0); return n.kind === 'DeclRefExpr' ? n : n.kind === 'MemberExpr' || n.kind === 'ArraySubscriptExpr' ? root(n.inner[0]) : (CAST.has(n.kind) && n.castKind === 'ArrayToPointerDecay') ? root(n.inner[0]) : undefined; };
|
|
489
|
+
const scan = (n) => {
|
|
490
|
+
const w = n.kind === 'BinaryOperator' && n.opcode === '=' || n.kind === 'CompoundAssignOperator' ? root(n.inner[0]) : n.kind === 'UnaryOperator' && ['++', '--', '&'].includes(n.opcode) ? root(n.inner[0]) : undefined;
|
|
491
|
+
const d = w?.referencedDecl;
|
|
492
|
+
if (d?.kind === 'VarDecl' && globals.has(d.id))
|
|
493
|
+
writes.add(d.id);
|
|
494
|
+
if (n.kind === 'DeclRefExpr' && n.referencedDecl?.kind === 'VarDecl' && globals.has(n.referencedDecl.id)) {
|
|
495
|
+
reads.add(n.referencedDecl.id);
|
|
496
|
+
all.add(n.referencedDecl.id);
|
|
497
|
+
}
|
|
498
|
+
for (const c of n.inner ?? [])
|
|
499
|
+
scan(c);
|
|
500
|
+
};
|
|
501
|
+
for (const [name, f] of funcs) {
|
|
502
|
+
const r0 = new Set(reads);
|
|
503
|
+
reads.clear();
|
|
504
|
+
scan(f);
|
|
505
|
+
fnReads.set(name, new Set(reads));
|
|
506
|
+
for (const id of r0)
|
|
507
|
+
reads.add(id);
|
|
508
|
+
}
|
|
509
|
+
// (and the functions each one calls, read through)
|
|
510
|
+
const callees = (f, out = new Set()) => { const w = (n) => { if (n.kind === 'DeclRefExpr' && n.referencedDecl?.kind === 'FunctionDecl' && funcs.has(n.referencedDecl.name) && !out.has(n.referencedDecl.name)) {
|
|
511
|
+
out.add(n.referencedDecl.name);
|
|
512
|
+
w(funcs.get(n.referencedDecl.name));
|
|
513
|
+
} for (const c of n.inner ?? [])
|
|
514
|
+
w(c); }; w(f); return out; };
|
|
515
|
+
for (const [name, f] of funcs)
|
|
516
|
+
for (const g of callees(f))
|
|
517
|
+
for (const id of fnReads.get(g) ?? [])
|
|
518
|
+
fnReads.get(name).add(id);
|
|
519
|
+
for (const [id, g] of globals) {
|
|
520
|
+
if (!all.has(id))
|
|
521
|
+
continue;
|
|
522
|
+
if (leave.includes(g.name)) {
|
|
523
|
+
left.add(id);
|
|
524
|
+
continue;
|
|
525
|
+
}
|
|
526
|
+
const qt = g.type.qualType;
|
|
527
|
+
if (/^const /.test(qt) && !writes.has(id))
|
|
528
|
+
continue;
|
|
529
|
+
if (/\[\]$/.test(qt.trim()))
|
|
530
|
+
refuse(g, `${g.name}: an array of unknown size (its length is another file's)`);
|
|
531
|
+
if (!writes.has(id) && (g.storageClass === 'extern' || /\bvolatile\b/.test(qt) || g.storageClass !== 'static'))
|
|
532
|
+
ginput.add(id);
|
|
533
|
+
gfield.set(id, gnodes.length);
|
|
534
|
+
gnodes.push(g);
|
|
535
|
+
}
|
|
536
|
+
if (!gnodes.length)
|
|
537
|
+
refuse(undefined, `no global of ${base} its functions read or write (the state, when the file keeps it in globals)`);
|
|
538
|
+
}
|
|
539
|
+
const entryT = entryRec ? recT(entryRec, entry) : gmode ? add(`globals:${entry}`, { k: 'struct', own: true, name: entry, go: 'gdp_G', fields: gnodes.map(g => ({ name: g.name, t: tyOf(g.type), embedded: false })) }) : refuse(undefined, `no ${cpp ? 'class' : 'struct'} ${entry} in ${base}${cpp ? ' (a class template is outside the subset)' : ''}${!cpp && entry !== stem ? ` (a file that keeps its state in globals: name ${stem}, its stem)` : ''}`);
|
|
540
|
+
const ptrT = add(`ptr:${entryT}`, { k: 'ptr', go: `${entry} *`, elem: entryT }), self0 = (n) => ({ e: 'id', ...at(n, ptrT), o: 'var', id: 'this', n: 'this' });
|
|
388
541
|
// An out-parameter (a pointer to an integer or a bool, not the receiver): a pointer to a box of one field, `value`,
|
|
389
542
|
// *p its field.
|
|
390
|
-
const boxed = new Map(), byValue = new Map(), refs = new Set();
|
|
543
|
+
const boxed = new Map(), byValue = new Map(), refs = new Set(), structRefs = new Set();
|
|
391
544
|
const boxOf = (q) => {
|
|
392
545
|
// (a const reference to an integer is its value: an input)
|
|
393
546
|
if (/^const [^*&]*&$/.test(q.type.qualType.trim())) {
|
|
@@ -396,6 +549,13 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
396
549
|
return v;
|
|
397
550
|
}
|
|
398
551
|
const ref = /[^&]&$/.test(q.type.qualType.trim()), t = ref ? parseT(q.type.qualType.trim().slice(0, -1), q.type.desugaredQualType?.trim().replace(/&$/, '')) : tyOf(q.type), pt = ref ? { k: 'ptr', elem: t, go: q.type.qualType } : T(t);
|
|
552
|
+
// (C++: a reference to a struct, the caller's object, read as a pointer to it)
|
|
553
|
+
if (ref && T(t).k === 'struct') {
|
|
554
|
+
const r = add(`ref:${t}`, { k: 'ptr', go: q.type.qualType, elem: t });
|
|
555
|
+
byValue.set(q.id, r);
|
|
556
|
+
structRefs.add(q.id);
|
|
557
|
+
return r;
|
|
558
|
+
}
|
|
399
559
|
if (pt.k !== 'ptr' || !['int', 'bool'].includes(T(pt.elem).k))
|
|
400
560
|
return tyOf(q.type);
|
|
401
561
|
if (ref)
|
|
@@ -405,9 +565,10 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
405
565
|
return bp;
|
|
406
566
|
};
|
|
407
567
|
const THIS = { kind: 'ParmVarDecl', id: 'this', name: 'this', type: { qualType: `${entry} *` } };
|
|
408
|
-
const recvOf = (f) => { if (
|
|
568
|
+
const recvOf = (f) => { if (gmode)
|
|
569
|
+
return THIS; if (f.$m)
|
|
409
570
|
return f.storageClass === 'static' ? undefined : THIS; const p = params(f)[0], t = p ? T(tyOf(p.type)) : undefined; return t?.k === 'ptr' && t.elem === entryT ? p : undefined; };
|
|
410
|
-
const args0 = (f) => (f.$m ? params(f) : params(f).slice(recvOf(f) ? 1 : 0));
|
|
571
|
+
const args0 = (f) => (f.$m || gmode ? params(f) : params(f).slice(recvOf(f) ? 1 : 0));
|
|
411
572
|
for (const f of [...funcs.values(), ...ctors])
|
|
412
573
|
for (const q of args0(f))
|
|
413
574
|
boxOf(q);
|
|
@@ -446,9 +607,30 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
446
607
|
return !!n.type && T(tyOf(n.type)).signed === false;
|
|
447
608
|
};
|
|
448
609
|
const calleeName = (n) => { const c = strip(n.inner[0]); const d = (c.kind === 'ImplicitCastExpr' ? strip(c.inner[0]) : c).referencedDecl; return d?.kind === 'FunctionDecl' ? d.name : undefined; };
|
|
610
|
+
// (a pointer flag's field, read where a flag is: tested, compared with NULL)
|
|
611
|
+
const flagOf = (n0) => { let n = strip(n0); while (CAST.has(n.kind) && ['BitCast', 'NoOp', 'LValueToRValue'].includes(n.castKind))
|
|
612
|
+
n = strip(n.inner[0]); const f = n.kind === 'MemberExpr' ? fields.get(n.referencedMemberDecl) : undefined; return f && T(T(recT(f.rec)).fields[f.i].t).nullflag ? n : undefined; };
|
|
613
|
+
const isNull = (n0) => { const n = strip(n0); return (CAST.has(n.kind) && (n.castKind === 'NullToPointer' || ((n.castKind === 'IntegralToPointer' || n.castKind === 'BitCast') && isNull(n.inner[0])))) || fold(n) === 0 || n.kind === 'CXXNullPtrLiteralExpr' || n.kind === 'GNUNullExpr'; };
|
|
614
|
+
let flagRead = false;
|
|
615
|
+
const readFlag = (n) => { flagRead = true; try {
|
|
616
|
+
return x(n);
|
|
617
|
+
}
|
|
618
|
+
finally {
|
|
619
|
+
flagRead = false;
|
|
620
|
+
} };
|
|
449
621
|
// A condition: a boolean as it is; a number compared with 0; a pointer with NULL.
|
|
450
622
|
const cond = (n0) => {
|
|
451
623
|
const n = strip(n0);
|
|
624
|
+
const fl = flagOf(n.kind === 'ImplicitCastExpr' && n.castKind === 'PointerToBoolean' ? n.inner[0] : n);
|
|
625
|
+
if (fl)
|
|
626
|
+
return readFlag(fl);
|
|
627
|
+
if (n.kind === 'BinaryOperator' && (n.opcode === '==' || n.opcode === '!=') && (flagOf(n.inner[0]) || flagOf(n.inner[1]))) {
|
|
628
|
+
const [f, o] = flagOf(n.inner[0]) ? [flagOf(n.inner[0]), n.inner[1]] : [flagOf(n.inner[1]), n.inner[0]];
|
|
629
|
+
if (!isNull(o))
|
|
630
|
+
return no(n, `${text(n)}: a pointer flag compared with other than NULL`);
|
|
631
|
+
const v = readFlag(f);
|
|
632
|
+
return n.opcode === '!=' ? v : { e: 'un', ...at(n, boolT), op: '!', x: v };
|
|
633
|
+
}
|
|
452
634
|
if (n.kind === 'CallExpr' && calleeName(n) === '__builtin_expect')
|
|
453
635
|
return cond(n.inner[1]);
|
|
454
636
|
if ((n.kind === 'ImplicitCastExpr' || n.kind === 'CStyleCastExpr') && ['IntegralCast', 'IntegralToBoolean', 'PointerToBoolean'].includes(n.castKind) && isBoolish(n.inner[0]))
|
|
@@ -456,7 +638,7 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
456
638
|
if (n.kind === 'BinaryOperator' && CMP.has(n.opcode))
|
|
457
639
|
return { e: 'bin', ...at(n, boolT), op: n.opcode, x: x(n.inner[0]), y: x(n.inner[1]) };
|
|
458
640
|
if (n.kind === 'BinaryOperator' && (n.opcode === '&&' || n.opcode === '||'))
|
|
459
|
-
return { e: 'bin', ...at(n, boolT), op: n.opcode, x: cond(n.inner[0]), y: cond(n.inner[1]) };
|
|
641
|
+
return { e: 'bin', ...at(n, boolT), op: n.opcode, x: cond(n.inner[0]), y: guard(() => cond(n.inner[1])) };
|
|
460
642
|
if (n.kind === 'UnaryOperator' && n.opcode === '!')
|
|
461
643
|
return { e: 'un', ...at(n, boolT), op: '!', x: cond(n.inner[0]) };
|
|
462
644
|
const v = (n.kind === 'ImplicitCastExpr' && (n.castKind === 'IntegralToBoolean' || n.castKind === 'PointerToBoolean')) ? n.inner[0] : n, vt = v.type ? tyOf(v.type) : -1;
|
|
@@ -471,8 +653,107 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
471
653
|
return { e: 'bin', ...at(n, boolT), op: '!=', x: x(v), y: lit(n, 0, vt) };
|
|
472
654
|
return no(n, `${text(n)}: a condition of type ${n.type?.qualType}`);
|
|
473
655
|
};
|
|
656
|
+
// A side effect inside an expression (an assignment, ++ or --, `if (--timer > 0)`): its statement run before the
|
|
657
|
+
// statement it is in, the expression its value; where it runs only on some paths (the right of && or ||, a branch of
|
|
658
|
+
// ?:), refused.
|
|
659
|
+
let pending, guarded = 0, temps = 0;
|
|
660
|
+
const guard = (f) => { guarded++; try {
|
|
661
|
+
return f();
|
|
662
|
+
}
|
|
663
|
+
finally {
|
|
664
|
+
guarded--;
|
|
665
|
+
} };
|
|
666
|
+
const effect = (n, value, before = []) => {
|
|
667
|
+
if (!pending || guarded)
|
|
668
|
+
return no(n, `${text(n)}: ${guarded ? 'a side effect on some paths of an expression (the right of && or ||, a branch of ?:)' : 'a side effect inside an expression (the subset reads it as a statement)'}`);
|
|
669
|
+
pending.push(...before, ...st(n));
|
|
670
|
+
return value();
|
|
671
|
+
};
|
|
672
|
+
const pre = (f) => { const outer = pending, og = guarded; pending = []; guarded = 0; try {
|
|
673
|
+
const v = f();
|
|
674
|
+
return [pending, v];
|
|
675
|
+
}
|
|
676
|
+
finally {
|
|
677
|
+
pending = outer;
|
|
678
|
+
guarded = og;
|
|
679
|
+
} };
|
|
680
|
+
// (a float's value: left out, a value nothing the rule keeps may read; one read into an integer, a condition or a
|
|
681
|
+
// comparison is refused there. Its operands are the code's measurements: an assignment or a call of the file's own
|
|
682
|
+
// inside it is refused, its effect would be lost.)
|
|
683
|
+
// A constant table read at indices (a transition table, a lookup; a const or constexpr array, a class's static one):
|
|
684
|
+
// its entry by cases on the indices, each index outside its dimension undefined behavior, the step's failure (checked
|
|
685
|
+
// before the statement, so the index must be free of side effects and read where the statement surely reads it).
|
|
686
|
+
const table = (n) => {
|
|
687
|
+
const idx = [];
|
|
688
|
+
let b = n;
|
|
689
|
+
while (b.kind === 'ArraySubscriptExpr') {
|
|
690
|
+
idx.unshift(b.inner[1]);
|
|
691
|
+
b = strip(b.inner[0]);
|
|
692
|
+
if (CAST.has(b.kind) && b.castKind === 'ArrayToPointerDecay')
|
|
693
|
+
b = strip(b.inner[0]);
|
|
694
|
+
}
|
|
695
|
+
const d = b.kind === 'DeclRefExpr' ? b.referencedDecl : undefined;
|
|
696
|
+
if (d?.kind !== 'VarDecl' || !globals.has(d.id))
|
|
697
|
+
return undefined;
|
|
698
|
+
const g0 = globals.get(d.id), g = g0.init ? g0 : [...globals.values()].find(v => v.name === g0.name && v.init) ?? g0, qt = g.type.qualType;
|
|
699
|
+
if (!/^(const |constexpr )|\bconst\b/.test(qt) || !g.init)
|
|
700
|
+
return undefined;
|
|
701
|
+
const init = (g.inner ?? []).find(c => !c.kind.endsWith('Attr'));
|
|
702
|
+
if (!init)
|
|
703
|
+
return undefined;
|
|
704
|
+
const t = n.type ? tyOf(n.type) : -1, a = at(n, t);
|
|
705
|
+
// (the table's values, nested by dimension; an entry not given is 0)
|
|
706
|
+
const dims = [...qt.matchAll(/\[(\d+)\]/g)].map(m => Number(m[1]));
|
|
707
|
+
if (dims.length !== idx.length || !['int', 'bool'].includes(T(t).k))
|
|
708
|
+
return no(n, `${text(n)}: a table read other than at every one of its dimensions`);
|
|
709
|
+
const val = (v, path, k) => {
|
|
710
|
+
if (k === dims.length) {
|
|
711
|
+
const f = v ? fold(v) : 0;
|
|
712
|
+
return f ?? NaN;
|
|
713
|
+
}
|
|
714
|
+
const s0 = v ? strip(v) : undefined, kids = s0?.kind === 'InitListExpr' ? (s0.inner ?? []) : [];
|
|
715
|
+
return val(kids[path[k]], path, k + 1);
|
|
716
|
+
};
|
|
717
|
+
if (idx.some(effects))
|
|
718
|
+
return no(n, `${text(n)}: a table read at an index with a side effect`);
|
|
719
|
+
if (!pending || guarded)
|
|
720
|
+
return no(n, `${text(n)}: a table read on some paths of an expression (the right of && or ||, a branch of ?:)`);
|
|
721
|
+
const ix = idx.map(i => x(i)), ty = (i) => (ix[i].t >= 0 ? ix[i].t : intT('int', target.int, true));
|
|
722
|
+
const inRange = ix.map((v, i) => ({ e: 'bin', ...at(idx[i], boolT), op: '&&', x: { e: 'bin', ...at(idx[i], boolT), op: '<=', x: lit(idx[i], 0, ty(i)), y: v }, y: { e: 'bin', ...at(idx[i], boolT), op: '<', x: v, y: lit(idx[i], dims[i], ty(i)) } }));
|
|
723
|
+
pending.push({ s: 'if', p: pos(n), c: { e: 'un', ...at(n, boolT), op: '!', x: inRange.reduce((l, r) => ({ e: 'bin', ...at(n, boolT), op: '&&', x: l, y: r })) }, then: [{ s: 'expr', p: pos(n), x: { e: 'call', ...at(n, -1), builtin: 'panic', a: [], what: `${text(n)}: an index past the table (undefined behavior)` } }] });
|
|
724
|
+
// (by cases: each dimension's index against each of its values, the last value the rest)
|
|
725
|
+
const build = (k, path) => {
|
|
726
|
+
if (k === dims.length) {
|
|
727
|
+
const v = val(init, path, 0);
|
|
728
|
+
if (!Number.isSafeInteger(v))
|
|
729
|
+
return no(n, `${text(n)}: an entry that isn't a constant integer`);
|
|
730
|
+
return T(t).k === 'bool' ? { e: 'lit', ...a, c: { bool: v !== 0 } } : lit(n, v, t);
|
|
731
|
+
}
|
|
732
|
+
let out = build(k + 1, [...path, dims[k] - 1]);
|
|
733
|
+
for (let j = dims[k] - 2; j >= 0; j--)
|
|
734
|
+
out = { e: 'ite', ...a, x: { e: 'bin', ...at(idx[k], boolT), op: '==', x: ix[k], y: lit(idx[k], j, ty(k)) }, y: build(k + 1, [...path, j]), b: out };
|
|
735
|
+
return out;
|
|
736
|
+
};
|
|
737
|
+
return build(0, []);
|
|
738
|
+
};
|
|
739
|
+
let placing = false;
|
|
740
|
+
// (a place in a global left out, or its address)
|
|
741
|
+
// (a field of the entry left out, `leave`, or anything reached through one: an object other code owns, a buffer the
|
|
742
|
+
// code writes for it)
|
|
743
|
+
const leftField = (n) => { const f = fields.get(n.referencedMemberDecl ?? ''); return !!f && f.rec === entryRec && leave.includes(String(n.name)); };
|
|
744
|
+
const leftOut = (n0) => { const n = strip(n0); if (n.kind === 'DeclRefExpr')
|
|
745
|
+
return left.has(n.referencedDecl?.id ?? ''); if (n.kind === 'MemberExpr' && leftField(n))
|
|
746
|
+
return true; if (n.kind === 'MemberExpr' || n.kind === 'ArraySubscriptExpr' || (n.kind === 'UnaryOperator' && n.opcode === '&') || (CAST.has(n.kind) && n.castKind === 'ArrayToPointerDecay'))
|
|
747
|
+
return leftOut(n.inner[0]); return false; };
|
|
748
|
+
const effects = (n) => (n.kind === 'CallExpr' && funcs.has(calleeName(n) ?? '')) || ['CompoundAssignOperator'].includes(n.kind) || (n.kind === 'BinaryOperator' && n.opcode === '=') || (n.kind === 'UnaryOperator' && ['++', '--'].includes(n.opcode)) || (n.inner ?? []).some(effects);
|
|
474
749
|
const x = (n0) => {
|
|
475
750
|
const n = strip(n0), t = n.type ? tyOf(n.type) : -1, a = at(n, t), kids = n.inner ?? [];
|
|
751
|
+
const asPlace = placing;
|
|
752
|
+
placing = false;
|
|
753
|
+
if ((left.size || leave.length) && leftOut(n))
|
|
754
|
+
return { e: 'opaque', ...a, why: `${text(n)}: left out (leave: an output for other code, never read into the rule)` };
|
|
755
|
+
if (t >= 0 && T(t).k === 'float' && !asPlace)
|
|
756
|
+
return effects(n) ? no(n, `${text(n)}: a float computed with an effect of the file's own code (floats are left out)`) : { e: 'opaque', ...a, why: `${text(n)}: a float (left out of the rule: a binary fraction that rounds)` };
|
|
476
757
|
const k = t >= 0 && T(t).k === 'int' ? fold(n) : undefined;
|
|
477
758
|
if (k !== undefined)
|
|
478
759
|
return Number.isSafeInteger(k) ? lit(n, k, t) : no(n, `${text(n)}: the constant ${n.kind === 'IntegerLiteral' ? n.value : k} is past 2^53 (the rule's numbers stop there)`);
|
|
@@ -491,12 +772,19 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
491
772
|
}
|
|
492
773
|
case 'DeclRefExpr': {
|
|
493
774
|
const d = n.referencedDecl;
|
|
775
|
+
// (a reference to the caller's struct: the object it points to)
|
|
776
|
+
if (d.kind === 'ParmVarDecl' && structRefs.has(d.id))
|
|
777
|
+
return { e: 'star', ...a, x: { e: 'id', ...a, t: byValue.get(d.id), o: 'var', id: d.id, n: d.name } };
|
|
494
778
|
if (d.kind === 'ParmVarDecl') {
|
|
495
779
|
const v = { e: 'id', ...a, ...(boxed.has(d.id) ? { t: boxed.get(d.id) } : {}), o: 'var', id: d.id, n: d.name };
|
|
496
780
|
return refs.has(d.id) ? { e: 'sel', ...a, k: 'field', x: v, path: [0] } : v;
|
|
497
781
|
}
|
|
498
782
|
if (d.kind === 'VarDecl' && /&$/.test(d.type?.qualType ?? ''))
|
|
499
783
|
return no(n, `${d.name}: a reference held in a local (a second name for a place)`);
|
|
784
|
+
if (d.kind === 'VarDecl' && gfield.has(d.id))
|
|
785
|
+
return { e: 'sel', ...a, k: 'field', x: self0(n), path: [gfield.get(d.id)] };
|
|
786
|
+
if (d.kind === 'VarDecl' && left.has(d.id))
|
|
787
|
+
return { e: 'opaque', ...a, why: `${d.name}: left out (leave: an output for other code, never read into the rule)` };
|
|
500
788
|
if (d.kind === 'VarDecl')
|
|
501
789
|
return globals.has(d.id) ? no(n, `${d.name}: a ${globals.get(d.id).storageClass === 'static' ? 'static' : 'global'} variable (state every object shares)`) : { e: 'id', ...a, o: 'var', id: d.id, n: d.name };
|
|
502
790
|
if (d.kind === 'FunctionDecl')
|
|
@@ -505,6 +793,8 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
505
793
|
}
|
|
506
794
|
case 'MemberExpr': {
|
|
507
795
|
const f = fields.get(n.referencedMemberDecl);
|
|
796
|
+
if (f && !flagRead && !asPlace && T(T(recT(f.rec)).fields[f.i].t).nullflag)
|
|
797
|
+
return no(n, `${text(n)}: a void * read as a pointer (the subset reads one as a flag: set to NULL or not, tested against NULL)`);
|
|
508
798
|
if (!f)
|
|
509
799
|
return no(n, `${text(n)}: a member that isn't a field of a struct read here`);
|
|
510
800
|
if (records.get(f.rec).tagUsed === 'union')
|
|
@@ -533,6 +823,10 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
533
823
|
return x(inner);
|
|
534
824
|
if (ck === 'BitCast' && T(t).k === 'ptr' && strip(inner).type && tyOf(strip(inner).type) === t)
|
|
535
825
|
return x(inner);
|
|
826
|
+
// (a pointer to the state made a void * or another pointer type, given to other code: still a pointer to the
|
|
827
|
+
// state, which that code may change; anything read through it here is refused)
|
|
828
|
+
if (ck === 'BitCast' && T(t).k === 'ptr' && strip(inner).type && T(tyOf(strip(inner).type)).k === 'ptr')
|
|
829
|
+
return x(inner);
|
|
536
830
|
return no(n, `${text(n)}: a ${ck} conversion${ck === 'IntegralToFloating' || ck.includes('Floating') ? ' (floats are outside the subset)' : ck.includes('Pointer') ? ' (a pointer made from other than the state\'s objects)' : ''}`);
|
|
537
831
|
}
|
|
538
832
|
case 'BinaryOperator': {
|
|
@@ -554,6 +848,8 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
554
848
|
return { e: 'bin', ...a, op: op === '^' ? '^' : op, x: x(l), y: x(r) };
|
|
555
849
|
if (op === ',')
|
|
556
850
|
return no(n, 'the comma operator');
|
|
851
|
+
if (op === '=')
|
|
852
|
+
return effect(n, () => x(l));
|
|
557
853
|
return no(n, `${text(n)}: an assignment inside an expression (the subset reads it as a statement)`);
|
|
558
854
|
}
|
|
559
855
|
case 'UnaryOperator': {
|
|
@@ -570,9 +866,17 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
570
866
|
const r = strip(kids[0]);
|
|
571
867
|
return r.kind === 'DeclRefExpr' && boxed.has(r.referencedDecl.id) ? { e: 'sel', ...a, k: 'field', x: x(r), path: [0] } : { e: 'star', ...a, x: x(kids[0]) };
|
|
572
868
|
}
|
|
869
|
+
// (x++ inside an expression: x's value before it, a local; ++x: x after it)
|
|
870
|
+
if (op === '++' || op === '--') {
|
|
871
|
+
if (!n.isPostfix)
|
|
872
|
+
return effect(n, () => x(kids[0]));
|
|
873
|
+
const id = `$t${++temps}`, v = x(kids[0]);
|
|
874
|
+
return effect(n, () => ({ e: 'id', ...a, o: 'var', id, n: id }), [{ s: 'decl', p: pos(n), specs: [{ names: [{ name: id, id, t: tyOf(kids[0].type) }], v: [v] }] }]);
|
|
875
|
+
}
|
|
573
876
|
return no(n, `${text(n)}: ${op} inside an expression (the subset reads it as a statement)`);
|
|
574
877
|
}
|
|
575
|
-
case 'ConditionalOperator': return { e: 'ite', ...a, x: cond(kids[0]), y: x(kids[1]), b: x(kids[2]) };
|
|
878
|
+
case 'ConditionalOperator': return { e: 'ite', ...a, x: cond(kids[0]), y: guard(() => x(kids[1])), b: guard(() => x(kids[2])) };
|
|
879
|
+
case 'CompoundAssignOperator': return effect(n, () => x(kids[0]));
|
|
576
880
|
case 'CallExpr': return call(n);
|
|
577
881
|
case 'CompoundLiteralExpr': return x(kids[0]);
|
|
578
882
|
case 'InitListExpr': {
|
|
@@ -586,6 +890,9 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
586
890
|
return { e: 'comp', ...a, elts: kids.flatMap((k, i) => (k.kind === 'ImplicitValueInitExpr' ? [] : [{ field: tt.fields[i].name, v: x(k) }])) };
|
|
587
891
|
}
|
|
588
892
|
case 'ArraySubscriptExpr': {
|
|
893
|
+
const tb = table(n);
|
|
894
|
+
if (tb)
|
|
895
|
+
return tb;
|
|
589
896
|
// An index into an array of the state (its decay to a pointer undone); pointer arithmetic is outside.
|
|
590
897
|
const b = strip(kids[0]), arr = b.kind === 'ImplicitCastExpr' && b.castKind === 'ArrayToPointerDecay' ? b.inner[0] : undefined;
|
|
591
898
|
return arr ? { e: 'index', ...a, x: place(arr), i: x(kids[1]) } : no(n, `${text(n)}: an index into a pointer (pointer arithmetic)`);
|
|
@@ -598,13 +905,31 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
598
905
|
};
|
|
599
906
|
// A place: a local, a field (through a pointer: ->), *p.
|
|
600
907
|
const place = (n0) => {
|
|
601
|
-
const n = strip(n0);
|
|
602
|
-
if (n.kind === 'DeclRefExpr' || n.kind === 'MemberExpr' || n.kind === 'CXXOperatorCallExpr' || (n.kind === 'UnaryOperator' && n.opcode === '*'))
|
|
908
|
+
const n = strip(n0), at0 = () => { const was = placing; placing = true; try {
|
|
603
909
|
return x(n);
|
|
910
|
+
}
|
|
911
|
+
finally {
|
|
912
|
+
placing = was;
|
|
913
|
+
} };
|
|
914
|
+
if (n.kind === 'DeclRefExpr' || n.kind === 'MemberExpr' || n.kind === 'CXXOperatorCallExpr' || (n.kind === 'UnaryOperator' && n.opcode === '*'))
|
|
915
|
+
return at0();
|
|
604
916
|
if (n.kind === 'ArraySubscriptExpr')
|
|
605
|
-
return
|
|
917
|
+
return at0();
|
|
606
918
|
return no(n, `${text(n)}: a place the subset doesn't write`);
|
|
607
919
|
};
|
|
920
|
+
// Other code called with different constants (CO_isError(em, BUS_OFF), CO_isError(em, HB_CONSUMER)): a question per
|
|
921
|
+
// constant arguments, each its own answer, named by them (CO_isError_x_18: x where an argument isn't a constant).
|
|
922
|
+
const asked = new Map();
|
|
923
|
+
const sig = (args) => args.map(a => { const k = fold(a); return k === undefined || !Number.isSafeInteger(k) ? 'x' : String(k).replace('-', 'm'); }).join('_');
|
|
924
|
+
const scanCalls = (n) => { if (n.kind === 'CallExpr') {
|
|
925
|
+
const nm = calleeName(n);
|
|
926
|
+
if (nm && (!funcs.has(nm) || cut.includes(nm)))
|
|
927
|
+
(asked.get(nm) ?? asked.set(nm, new Set()).get(nm)).add(sig((n.inner ?? []).slice(1)));
|
|
928
|
+
} for (const c of n.inner ?? [])
|
|
929
|
+
scanCalls(c); };
|
|
930
|
+
for (const f of funcs.values())
|
|
931
|
+
scanCalls(f);
|
|
932
|
+
const keyed = (name, args) => ((asked.get(name)?.size ?? 0) > 1 && /\d/.test(sig(args)) ? `${name}_${sig(args)}` : name);
|
|
608
933
|
const call = (n) => {
|
|
609
934
|
const t = n.type ? tyOf(n.type) : -1, a = at(n, t), name = calleeName(n), args = (n.inner ?? []).slice(1);
|
|
610
935
|
if (!name)
|
|
@@ -617,13 +942,16 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
617
942
|
return no(n, `${name}: the process ends`);
|
|
618
943
|
if (cpp && (name === 'min' || name === 'max') && args.length === 2)
|
|
619
944
|
return { e: 'call', ...a, builtin: name, a: args.map(x) };
|
|
620
|
-
|
|
945
|
+
// (one of the file's own the caller answers, `answers`: read as other code's, its value the request's)
|
|
946
|
+
const f = cut.includes(name) ? undefined : funcs.get(name);
|
|
621
947
|
if (f?.$m)
|
|
622
948
|
return no(n, `${name}: a static method`);
|
|
623
949
|
// (another file's or a library's function: at the boundary where it is inferred, with its end, where the check
|
|
624
950
|
// stubs an answered one in a copy of the file)
|
|
625
951
|
if (!f)
|
|
626
|
-
return { e: 'call', ...a, f: { e: 'sel', ...at(n, -1), k: 'qual', pkg: '', n: name, why: `${name}: a function of another file or library (other code)` }, a: args.map(x), ...(n.$e && own(n) ? { end: `${n.$e.line}:${n.$e.col + n.$e.tok}` } : {}) };
|
|
952
|
+
return { e: 'call', ...a, f: { e: 'sel', ...at(n, -1), k: 'qual', pkg: '', n: keyed(name, args), why: `${name}: a function of another file or library (other code)` }, a: args.map(x), ...(n.$e && own(n) ? { end: `${n.$e.line}:${n.$e.col + n.$e.tok}` } : {}) };
|
|
953
|
+
if (gmode)
|
|
954
|
+
return { e: 'call', ...a, f: { e: 'sel', ...at(n, -1), k: 'method', x: self0(n), n: name, mtype: entry, mptr: true }, a: args.map(x) };
|
|
627
955
|
if (recvOf(f) && args.length)
|
|
628
956
|
return { e: 'call', ...a, f: { e: 'sel', ...at(n, -1), k: 'method', x: x(args[0]), n: name, mtype: entry, mptr: true }, a: args.slice(1).map(x) };
|
|
629
957
|
return { e: 'call', ...a, f: { e: 'id', ...at(n, -1), o: 'func', n: name }, a: args.map(x) };
|
|
@@ -635,6 +963,12 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
635
963
|
const bt = b?.type ? T(tyOf(b.type)) : undefined;
|
|
636
964
|
if (me.name === 'size' && bt?.fixed)
|
|
637
965
|
return lit(n, bt.len, t);
|
|
966
|
+
// (another class's method with no body here: other code at the boundary, as a function of another file, its value
|
|
967
|
+
// the request's where it is read; from where the call begins, so the check stubs the whole call)
|
|
968
|
+
if (b && (!m || m.$rec !== entryRec) && m?.name && (!own(m) || (!(m.inner ?? []).some(c => c.kind === 'CompoundStmt') && !cxx.some(k => k.previousDecl === m.id)))) {
|
|
969
|
+
const p0 = n.$b && own(n) ? `${path.basename(n.$b.file)}:${n.$b.line}:${n.$b.col}` : pos(n);
|
|
970
|
+
return { e: 'call', ...at(n, t), p: p0, f: { e: 'sel', ...at(n, -1), k: 'qual', pkg: '', n: m.name, why: `${text(n)}: a method of another class (other code)` }, a: (n.inner ?? []).slice(1).map(x), ...(n.$e && own(n) ? { end: `${n.$e.line}:${n.$e.col + n.$e.tok}` } : {}) };
|
|
971
|
+
}
|
|
638
972
|
if (!m || m.$rec !== entryRec || !b)
|
|
639
973
|
return no(n, `${text(n)}: a method of another class (other code)`);
|
|
640
974
|
if (overloaded.has(m.name) || (picks[m.name] !== undefined && params(m).length !== picks[m.name]))
|
|
@@ -654,12 +988,19 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
654
988
|
const ends = (b) => { const l = b.at(-1); return !!l && (l.s === 'return' || l.s === 'panic' || (l.s === 'branch' && l.tok === 'break')); };
|
|
655
989
|
function st(n) {
|
|
656
990
|
const p = pos(n), kids = n.inner ?? [];
|
|
991
|
+
// (a write into a global left out: its value computed, the write not kept)
|
|
992
|
+
if ((left.size || leave.length) && (n.kind === 'CompoundAssignOperator' || (n.kind === 'BinaryOperator' && n.opcode === '=') || (n.kind === 'UnaryOperator' && ['++', '--'].includes(n.opcode))) && leftOut(kids[0])) {
|
|
993
|
+
if (n.kind === 'UnaryOperator')
|
|
994
|
+
return [];
|
|
995
|
+
const [ps, v] = pre(() => x(kids[1]));
|
|
996
|
+
return [...ps, { s: 'expr', p, x: v }];
|
|
997
|
+
}
|
|
657
998
|
switch (n.kind) {
|
|
658
999
|
case 'CompoundStmt': return [{ s: 'block', p, b: ss(kids) }];
|
|
659
1000
|
case 'NullStmt': return [];
|
|
660
1001
|
case 'DeclStmt': return kids.flatMap((d) => {
|
|
661
1002
|
if (d.kind !== 'VarDecl')
|
|
662
|
-
return
|
|
1003
|
+
return ['RecordDecl', 'TypedefDecl', 'EnumDecl', 'StaticAssertDecl', 'TypeAliasDecl', 'UsingDecl'].includes(d.kind) ? [] : sno(d, `a ${d.kind} in a function`);
|
|
663
1004
|
if (d.storageClass === 'static')
|
|
664
1005
|
return sno(d, `${d.name}: a static local (state every call shares)`);
|
|
665
1006
|
// std::lock_guard, std::scoped_lock, std::unique_lock of one of the object's mutexes, in the function's own
|
|
@@ -674,14 +1015,21 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
674
1015
|
const init = d.init ? (d.inner ?? []).find(c => !c.kind.endsWith('Attr')) : undefined;
|
|
675
1016
|
if (!init)
|
|
676
1017
|
return [];
|
|
677
|
-
|
|
1018
|
+
const [ps, v] = pre(() => x(init));
|
|
1019
|
+
return [...ps, { s: 'decl', p, specs: [{ names: [{ name: d.name, id: d.id, t: tyOf(d.type) }], v: [v] }] }];
|
|
678
1020
|
});
|
|
679
|
-
case 'ReturnStmt':
|
|
1021
|
+
case 'ReturnStmt': {
|
|
1022
|
+
if (!kids.length)
|
|
1023
|
+
return [{ s: 'return', p, r: [] }];
|
|
1024
|
+
const [ps, v] = pre(() => x(kids[0]));
|
|
1025
|
+
return [...ps, { s: 'return', p, r: [v] }];
|
|
1026
|
+
}
|
|
680
1027
|
case 'IfStmt': {
|
|
681
1028
|
if (n.hasInitStorage || n.hasVarStorage)
|
|
682
1029
|
return sno(n, 'an if with a declaration');
|
|
683
1030
|
const [c, th, el] = kids;
|
|
684
|
-
|
|
1031
|
+
const [ps, cc] = pre(() => cond(c));
|
|
1032
|
+
return [...ps, { s: 'if', p, c: cc, then: block(th), ...(el ? { else: { s: 'block', p: pos(el), b: block(el) } } : {}) }];
|
|
685
1033
|
}
|
|
686
1034
|
case 'SwitchStmt': return switchC(n);
|
|
687
1035
|
case 'BreakStmt': return [{ s: 'branch', p, tok: 'break' }];
|
|
@@ -691,7 +1039,20 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
691
1039
|
case 'CXXForRangeStmt': return sno(n, 'a loop (for over a range)');
|
|
692
1040
|
case 'WhileStmt':
|
|
693
1041
|
case 'DoStmt':
|
|
694
|
-
case 'ForStmt':
|
|
1042
|
+
case 'ForStmt': {
|
|
1043
|
+
// (an endless loop nothing leaves, an assertion's trap or a wait for the watchdog's reset: the code stops there,
|
|
1044
|
+
// the step's failure)
|
|
1045
|
+
const c0 = n.kind === 'ForStmt' ? (kids[2]?.kind ? kids[2] : undefined) : n.kind === 'DoStmt' ? kids[1] : kids.find(k => k.kind && k.kind !== 'NullStmt' && !k.kind.endsWith('Stmt'));
|
|
1046
|
+
const body = n.kind === 'DoStmt' ? kids[0] : kids.at(-1), leaves = (b) => ['BreakStmt', 'ReturnStmt', 'GotoStmt', 'IndirectGotoStmt'].includes(b.kind) || (b.inner ?? []).some(leaves) || (b.kind === 'CallExpr' && !FAIL[calleeName(b) ?? '']);
|
|
1047
|
+
const k = c0 && c0.kind !== 'NullStmt' ? fold(c0) : n.kind === 'ForStmt' ? 1 : undefined;
|
|
1048
|
+
if (k !== undefined && k !== 0 && body && !leaves(body))
|
|
1049
|
+
return [{ s: 'expr', p, x: { e: 'call', ...at(n, -1), builtin: 'panic', a: [], what: 'an endless loop (the code stops there)' } }];
|
|
1050
|
+
// (do { ... } while (0), a macro's one statement: its body once, where nothing in it breaks or continues it)
|
|
1051
|
+
const exits = (b) => ['BreakStmt', 'ContinueStmt'].includes(b.kind) || (!['WhileStmt', 'DoStmt', 'ForStmt', 'SwitchStmt'].includes(b.kind) && (b.inner ?? []).some(exits));
|
|
1052
|
+
if (n.kind === 'DoStmt' && k === 0 && body && !(body.inner ?? []).some(exits))
|
|
1053
|
+
return block(body);
|
|
1054
|
+
return sno(n, `a loop (${n.kind === 'WhileStmt' ? 'while' : n.kind === 'DoStmt' ? 'do' : 'for'})`);
|
|
1055
|
+
}
|
|
695
1056
|
case 'GotoStmt':
|
|
696
1057
|
case 'IndirectGotoStmt': return sno(n, 'goto');
|
|
697
1058
|
case 'LabelStmt': return sno(n, 'a label (goto)');
|
|
@@ -714,10 +1075,19 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
714
1075
|
if (n.opcode !== '=')
|
|
715
1076
|
break;
|
|
716
1077
|
const [l, r] = kids, rs = strip(r);
|
|
1078
|
+
// (a pointer flag set: NULL its false, a constant other than 0 its true)
|
|
1079
|
+
if (flagOf(l)) {
|
|
1080
|
+
const k = isNull(r) ? 0 : (() => { let v = strip(r); while (CAST.has(v.kind))
|
|
1081
|
+
v = strip(v.inner[0]); return fold(v); })();
|
|
1082
|
+
if (k === undefined)
|
|
1083
|
+
return sno(n, `${text(n)}: a pointer flag set to other than NULL or a constant`);
|
|
1084
|
+
return [{ s: 'assign', p, op: '=', l: [place(l)], r: [{ e: 'lit', ...at(r, boolT), c: { bool: k !== 0 } }] }];
|
|
1085
|
+
}
|
|
717
1086
|
// a = b = c: b set first, then a to b's value.
|
|
718
1087
|
if (rs.kind === 'BinaryOperator' && rs.opcode === '=')
|
|
719
1088
|
return [...st(rs), { s: 'assign', p, op: '=', l: [place(l)], r: [conv2(r, tyOf(l.type), x(rs.inner[0]))] }];
|
|
720
|
-
|
|
1089
|
+
const [ps, rv] = pre(() => x(r));
|
|
1090
|
+
return [...ps, { s: 'assign', p, op: '=', l: [place(l)], r: [rv] }];
|
|
721
1091
|
}
|
|
722
1092
|
case 'CompoundAssignOperator': {
|
|
723
1093
|
const [l, r] = kids, lt = tyOf(l.type), ct = tyOf(n.computeLHSType), op = n.opcode.slice(0, -1);
|
|
@@ -743,7 +1113,10 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
743
1113
|
return [{ s: 'assign', p, op: '=', l: [place(l)], r: [conv(n, lt, { e: 'bin', ...at(n, it), op: n.opcode[0], x: conv(l, it, x(l)), y: lit(n, 1, it) })] }];
|
|
744
1114
|
}
|
|
745
1115
|
case 'CallExpr':
|
|
746
|
-
case 'CXXMemberCallExpr':
|
|
1116
|
+
case 'CXXMemberCallExpr': {
|
|
1117
|
+
const [ps, cx] = pre(() => (n.kind === 'CallExpr' ? call(n) : memberCall(n)));
|
|
1118
|
+
return [...ps, { s: 'expr', p, x: cx }];
|
|
1119
|
+
}
|
|
747
1120
|
}
|
|
748
1121
|
if (n.kind.endsWith('Expr') || n.kind.endsWith('Literal') || n.kind.endsWith('Operator'))
|
|
749
1122
|
return [{ s: 'expr', p, x: x(n) }];
|
|
@@ -761,7 +1134,7 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
761
1134
|
if (c.inner.length > 2)
|
|
762
1135
|
return void cases.push({ p: pos(c), b: sno(c, 'a case range (a GNU extension)') });
|
|
763
1136
|
const v = fold(c.inner[0]);
|
|
764
|
-
cases.push({ p: pos(c), list: [v === undefined ? no(c, 'a case that isn\'t a constant') : lit(c, v, tt)], b: [] });
|
|
1137
|
+
cases.push({ p: pos(c), list: [v === undefined ? no(c, 'a case that isn\'t a constant') : !Number.isSafeInteger(v) ? no(c, 'a case past 2^53 (the rule\'s numbers stop there)') : lit(c, v, tt)], b: [] });
|
|
765
1138
|
return c.inner[1];
|
|
766
1139
|
}
|
|
767
1140
|
if (c.kind === 'DefaultStmt') {
|
|
@@ -794,26 +1167,57 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
794
1167
|
delete c.list;
|
|
795
1168
|
cases.forEach((c, i) => { if (i < cases.length - 1 && !ends(c.b))
|
|
796
1169
|
c.b.push({ s: 'branch', p: c.p, tok: 'fallthrough' }); });
|
|
797
|
-
|
|
1170
|
+
const [ps, tx] = pre(() => x(tag));
|
|
1171
|
+
return [...ps, { s: 'switch', p: pos(n), tag: tx, cases }];
|
|
798
1172
|
}
|
|
799
1173
|
// --- the functions ----------------------------------------------------------------------------------------------------
|
|
800
|
-
const out = [], td0 = () => records.get(entryRec);
|
|
801
|
-
|
|
1174
|
+
const out = [], td0 = () => (gmode ? top.find(n => mine(n)) ?? top[0] : records.get(entryRec));
|
|
1175
|
+
// (the requests: the file's non-static functions, or those named; each given the globals the request gives)
|
|
1176
|
+
const requests = new Set(gmode ? (Object.keys(picks).length || pickNames.length ? pickNames : [...funcs].filter(([, f]) => f.storageClass !== 'static' && mine(f)).map(([n]) => n)) : []);
|
|
1177
|
+
const fn = (name, f, pre0 = []) => {
|
|
1178
|
+
// (a request of the file's globals: each global the request gives set from its argument of that name first)
|
|
1179
|
+
const gin = gmode && requests.has(name) ? gnodes.filter(g => ginput.has(g.id) && fnReads.get(name)?.has(g.id)) : [];
|
|
1180
|
+
for (const q of params(f))
|
|
1181
|
+
if (gin.some(g => g.name === q.name))
|
|
1182
|
+
refuse(q, `${name}: its parameter ${q.name} has a global's name the request gives`);
|
|
1183
|
+
const pre = [...pre0, ...gin.map((g) => ({ s: 'assign', p: pos(f), op: '=', l: [{ e: 'sel', ...at(f, tyOf(g.type)), k: 'field', x: self0(f), path: [gfield.get(g.id)] }], r: [{ e: 'id', ...at(f, tyOf(g.type)), o: 'var', id: `in:${g.id}`, n: g.name }] }))];
|
|
802
1184
|
const r = recvOf(f), body = f.inner.find(c => c.kind === 'CompoundStmt'), rt = f.kind === 'CXXConstructorDecl' ? parseT('void') : parseT(f.type.qualType.slice(0, retEnd(f.type.qualType)));
|
|
803
1185
|
for (const c of body.inner ?? [])
|
|
804
1186
|
topStmts.add(c.id);
|
|
805
1187
|
return { name, p: pos(f), span: [f.$b?.line ?? 0, f.$e?.line ?? 0],
|
|
806
|
-
...(r ? { recv: { t: tyOf(r.type), ptr: true, type: entry, ...(r.name ? { name: r.name, id: r.id } : {}) } } : {}),
|
|
807
|
-
params: args0(f).map(q => ({ ...(q.name ? { name: q.name, id: q.id } : {}), t: boxed.get(q.id) ?? byValue.get(q.id) ?? tyOf(q.type), ts: q.type.qualType })),
|
|
1188
|
+
...(r ? { recv: { t: gmode ? ptrT : tyOf(r.type), ptr: true, type: entry, ...(r.name ? { name: r.name, id: r.id } : {}) } } : {}),
|
|
1189
|
+
params: [...args0(f).map(q => ({ ...(q.name ? { name: q.name, id: q.id } : {}), t: boxed.get(q.id) ?? byValue.get(q.id) ?? tyOf(q.type), ts: q.type.qualType })), ...gin.map(g => ({ name: g.name, id: `in:${g.id}`, t: tyOf(g.type), ts: g.type.qualType }))],
|
|
808
1190
|
results: T(rt).k === 'void' ? [] : [{ t: rt }], body: [...pre, ...ss(body.inner ?? [])],
|
|
809
1191
|
...(/\.\.\.\)$/.test(f.type.qualType) ? { params: [{ t: -1, variadic: true }] } : {}),
|
|
810
1192
|
static: f.storageClass === 'static', ...(f.virtual ? { why: `${name}: a virtual method (its dispatch)` } : {}) };
|
|
811
1193
|
};
|
|
1194
|
+
let ctor;
|
|
812
1195
|
for (const [name, f] of funcs)
|
|
813
1196
|
out.push(fn(name, f));
|
|
1197
|
+
// (the globals' start: each one's initializer, where it has one; C's zero where not)
|
|
1198
|
+
if (gmode) {
|
|
1199
|
+
const pre = [];
|
|
1200
|
+
for (const g of gnodes) {
|
|
1201
|
+
const v = g.init ? (g.inner ?? []).find(c => !c.kind.endsWith('Attr')) : undefined, t = tyOf(g.type);
|
|
1202
|
+
const empty = !v || (strip(v).kind === 'InitListExpr' && (strip(v).inner ?? []).every(c => c.kind === 'ImplicitValueInitExpr'));
|
|
1203
|
+
// (C's zero, leaf by leaf: each integer and boolean of it 0, an array a list of zeros already)
|
|
1204
|
+
const zeros = (tt, place) => {
|
|
1205
|
+
const k = T(tt).k;
|
|
1206
|
+
if (k === 'int' || k === 'bool')
|
|
1207
|
+
pre.push({ s: 'assign', p: pos(g), op: '=', l: [place], r: [k === 'int' ? lit(g, 0, tt) : { e: 'lit', ...at(g, tt), c: { bool: false } }] });
|
|
1208
|
+
else if (k === 'struct')
|
|
1209
|
+
T(tt).fields.forEach((f, i) => zeros(f.t, { e: 'sel', ...at(g, f.t), k: 'field', x: place, path: [i] }));
|
|
1210
|
+
};
|
|
1211
|
+
const place = { e: 'sel', ...at(v ?? g, t), k: 'field', x: self0(v ?? g), path: [gfield.get(g.id)] };
|
|
1212
|
+
if (empty)
|
|
1213
|
+
zeros(t, place);
|
|
1214
|
+
else
|
|
1215
|
+
pre.push({ s: 'assign', p: pos(v), op: '=', l: [place], r: [x(v)] });
|
|
1216
|
+
}
|
|
1217
|
+
ctor = { name: '$ctor', p: pos(td0()), span: [0, 0], recv: { t: ptrT, ptr: true, type: entry, name: 'this', id: 'this' }, params: [], results: [], body: pre };
|
|
1218
|
+
}
|
|
814
1219
|
// The constructor (C++): its member initializers (each field's own in-class one where it has no other), then its body;
|
|
815
1220
|
// the rule starts where it leaves the object, its parameters the rule's.
|
|
816
|
-
let ctor;
|
|
817
1221
|
if (cpp) {
|
|
818
1222
|
if (ctors.length > 1)
|
|
819
1223
|
refuse(ctors[1], `${entry}: ${ctors.length} constructors (the C++ front end reads a class with one)`);
|
|
@@ -841,11 +1245,14 @@ export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
|
841
1245
|
const td = td0();
|
|
842
1246
|
const pkg = { lang: cpp ? 'cpp' : 'c', package: entry, files: [{ name: base, imports: [] }], funcs: out, types: [{ name: entry, t: entryT, p: pos(td), span: [td.$b?.line ?? 0, td.$e?.line ?? 0] }], consts: [], vars: [], errors: [], ttab, ...(ctor ? { ctor } : {}) };
|
|
843
1247
|
// (by default: the file's non-static functions taking the struct; a class's public methods)
|
|
844
|
-
const methods = out.filter((f, i) => { const n = [...funcs.values()][i]; return f.recv && !f.static && (n.$m ? n.$access === 'public' : mine(n)); }).map(f => f.name);
|
|
1248
|
+
const methods = gmode ? [...requests] : out.filter((f, i) => { const n = [...funcs.values()][i]; return f.recv && !f.static && (n.$m ? n.$access === 'public' : mine(n)); }).map(f => f.name);
|
|
845
1249
|
// (the class's qualified name, which the harness spells: `this`'s type, as clang prints it)
|
|
846
1250
|
const thisOf = (n) => (n.kind === 'CXXThisExpr' ? n.type.qualType : (n.inner ?? []).map(thisOf).find(Boolean));
|
|
847
1251
|
const self = cpp ? cxx.filter(m => m.$rec === entryRec).map(thisOf).find(Boolean)?.replace(/^const /, '').replace(/ \*$/, '') : undefined;
|
|
848
|
-
return { pkg, methods, entry, cname: self ?? T(entryT).go
|
|
1252
|
+
return { pkg, methods, entry, cname: self ?? T(entryT).go, consts: Object.fromEntries([...named].filter(([, v]) => Number.isSafeInteger(v))),
|
|
1253
|
+
...(gmode ? { globals: { state: gnodes.map(g => g.name), inputs: gnodes.filter(g => ginput.has(g.id)).map(g => g.name), types: Object.fromEntries(gnodes.map(g => [g.name, g.type.qualType])),
|
|
1254
|
+
// (declared only, another file's to define: the check defines it, zero at the start)
|
|
1255
|
+
externs: gnodes.filter(g => ![...globals.values()].some(d => d.name === g.name && (d.storageClass !== 'extern' || d.init))).map(g => g.name) } } : {}) };
|
|
849
1256
|
}
|
|
850
1257
|
// Where a function type's result ends: before its parameter list (the last parenthesized group).
|
|
851
1258
|
function retEnd(s) {
|