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.
- package/README.md +82 -6
- package/dist/core/c/reader.d.ts +86 -0
- package/dist/core/c/reader.js +860 -0
- package/dist/core/cli.d.ts +66 -0
- package/dist/core/cli.js +582 -0
- package/dist/core/dsl.d.ts +4 -2
- package/dist/core/dsl.js +22 -12
- package/dist/core/effect.d.ts +6 -4
- package/dist/core/effect.js +48 -10
- package/dist/core/ex/harness.exs +177 -0
- package/dist/core/ex/json.exs +46 -0
- package/dist/core/ex/reader.exs +462 -0
- package/dist/core/exists.d.ts +3 -0
- package/dist/core/exists.js +3 -1
- package/dist/core/extract-c.d.ts +35 -0
- package/dist/core/extract-c.js +424 -0
- package/dist/core/extract-check.js +27 -6
- package/dist/core/extract-ex.d.ts +16 -0
- package/dist/core/extract-ex.js +33 -0
- package/dist/core/extract-go-check.d.ts +28 -0
- package/dist/core/extract-go-check.js +639 -0
- package/dist/core/extract-go.d.ts +231 -0
- package/dist/core/extract-go.js +1166 -0
- package/dist/core/extract-jvm.d.ts +23 -0
- package/dist/core/extract-jvm.js +220 -0
- package/dist/core/extract-print.js +17 -7
- package/dist/core/extract-py.d.ts +15 -0
- package/dist/core/extract-py.js +31 -0
- package/dist/core/extract-rb.d.ts +17 -0
- package/dist/core/extract-rb.js +31 -0
- package/dist/core/extract-read.d.ts +20 -0
- package/dist/core/extract-read.js +46 -6
- package/dist/core/extract-rs.d.ts +12 -0
- package/dist/core/extract-rs.js +306 -0
- package/dist/core/extract-shapes.d.ts +2 -0
- package/dist/core/extract-shapes.js +6 -5
- package/dist/core/extract-step.d.ts +806 -0
- package/dist/core/extract-step.js +4141 -0
- package/dist/core/extract-swift.d.ts +27 -0
- package/dist/core/extract-swift.js +484 -0
- package/dist/core/extract-xstate.d.ts +10 -3
- package/dist/core/extract-xstate.js +50 -4
- package/dist/core/extract.d.ts +22 -3
- package/dist/core/extract.js +107 -18
- package/dist/core/go/go.mod +3 -0
- package/dist/core/go/reader.go +901 -0
- package/dist/core/infer.js +1770 -52
- package/dist/core/jvm/Harness.java +361 -0
- package/dist/core/jvm/JavaReader.java +825 -0
- package/dist/core/jvm/Jvm.java +332 -0
- package/dist/core/jvm/KtReader.java +960 -0
- package/dist/core/jvm/Rt.java +10 -0
- package/dist/core/laws.d.ts +66 -0
- package/dist/core/laws.js +522 -0
- package/dist/core/py/harness.py +328 -0
- package/dist/core/py/reader.py +447 -0
- package/dist/core/rb/harness.rb +233 -0
- package/dist/core/rb/reader.rb +375 -0
- package/dist/core/rs/Cargo.lock +108 -0
- package/dist/core/rs/Cargo.toml +14 -0
- package/dist/core/rs/src/main.rs +681 -0
- package/dist/core/swift/Harness.swift +124 -0
- package/dist/core/swift/Main.swift +34 -0
- package/dist/core/swift/Package.swift +13 -0
- package/dist/core/swift/Sources/gdp-swiftreader/main.swift +1341 -0
- package/dist/kernel/cdcl.js +43 -18
- package/dist/kernel/epr.d.ts +35 -0
- package/dist/kernel/epr.js +1676 -0
- package/dist/kernel/prove.d.ts +44 -0
- package/dist/kernel/prove.js +668 -54
- package/dist/lean-kernel/emit.d.ts +1 -0
- package/dist/lean-kernel/emit.js +21 -4
- package/package.json +53 -6
|
@@ -0,0 +1,860 @@
|
|
|
1
|
+
import { Refusal } from "../extract-read.js";
|
|
2
|
+
// (consecutive JSON values in one string, each with where it ends)
|
|
3
|
+
class JSONDecoder {
|
|
4
|
+
decode(s, i) {
|
|
5
|
+
let d = 0, q = false, j = i;
|
|
6
|
+
for (; j < s.length; j++) {
|
|
7
|
+
const c = s[j];
|
|
8
|
+
if (q) {
|
|
9
|
+
if (c === '\\')
|
|
10
|
+
j++;
|
|
11
|
+
else if (c === '"')
|
|
12
|
+
q = false;
|
|
13
|
+
}
|
|
14
|
+
else if (c === '"')
|
|
15
|
+
q = true;
|
|
16
|
+
else if (c === '{' || c === '[')
|
|
17
|
+
d++;
|
|
18
|
+
else if ((c === '}' || c === ']') && --d === 0)
|
|
19
|
+
break;
|
|
20
|
+
}
|
|
21
|
+
return [JSON.parse(s.slice(i, j + 1)), j + 1];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const proc = globalThis.process;
|
|
25
|
+
export const node = (id) => { const m = proc?.getBuiltinModule?.(id); if (!m)
|
|
26
|
+
throw new Error(`the C front end runs clang through ${id} (Node 20.16 or later, or Bun)`); return m; };
|
|
27
|
+
// --- running clang ------------------------------------------------------------------------------------------------------
|
|
28
|
+
// (C++: only the declarations named after the class, `-ast-dump-filter`: its standard headers' are hundreds of MB;
|
|
29
|
+
// clang writes them one after another, its locations' state going on from one to the next)
|
|
30
|
+
export function clangDump(file, flags, clang = 'clang', cpp = false, filter) {
|
|
31
|
+
const cp = node('node:child_process'), lang = cpp ? ['-x', 'c++'] : ['-x', 'c'];
|
|
32
|
+
const run = (args) => {
|
|
33
|
+
try {
|
|
34
|
+
return cp.execFileSync(clang, args, { encoding: 'utf8', maxBuffer: 1 << 30, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
const x = e;
|
|
38
|
+
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
|
+
}
|
|
40
|
+
};
|
|
41
|
+
const dump = run([...lang, ...flags, '-Xclang', '-ast-dump=json', ...(filter ? ['-Xclang', `-ast-dump-filter=${filter}`] : []), '-fsyntax-only', file]), inner = [], dec = new JSONDecoder();
|
|
42
|
+
const tu = filter ? { kind: 'TranslationUnitDecl', inner } : JSON.parse(dump);
|
|
43
|
+
if (filter)
|
|
44
|
+
for (let i = dump.indexOf('{'); i >= 0; i = dump.indexOf('{', i)) {
|
|
45
|
+
const [o, e] = dec.decode(dump, i);
|
|
46
|
+
inner.push(o);
|
|
47
|
+
i = e;
|
|
48
|
+
}
|
|
49
|
+
const defs = run([...lang, ...flags, '-dM', '-E', '/dev/null']), def = (k) => Number(new RegExp(`#define ${k} (\\d+)`).exec(defs)?.[1] ?? 0);
|
|
50
|
+
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
|
+
}
|
|
52
|
+
// The project's flags for a file: from a compile_commands.json (its -I, -D, -std, -include and -f options), or given.
|
|
53
|
+
export function flagsFor(file, compileCommands) {
|
|
54
|
+
if (!compileCommands)
|
|
55
|
+
return [];
|
|
56
|
+
const fs = node('node:fs'), path = node('node:path');
|
|
57
|
+
const db = JSON.parse(fs.readFileSync(compileCommands, 'utf8'));
|
|
58
|
+
const e = db.find(x => path.resolve(x.directory, x.file) === path.resolve(file));
|
|
59
|
+
if (!e)
|
|
60
|
+
throw new Refusal(file, `${compileCommands} has no command for it`);
|
|
61
|
+
const args = e.arguments ?? (e.command ?? '').match(/(?:[^\s"']+|"[^"]*"|'[^']*')+/g).map(a => a.replace(/^["']|["']$/g, ''));
|
|
62
|
+
const out = [];
|
|
63
|
+
for (let i = 1; i < args.length; i++) {
|
|
64
|
+
const a = args[i];
|
|
65
|
+
if (/^-(I|D|U|include|isystem|iquote)$/.test(a)) {
|
|
66
|
+
const v = args[++i];
|
|
67
|
+
out.push(a, a === '-D' || a === '-U' ? v : path.resolve(e.directory, v));
|
|
68
|
+
}
|
|
69
|
+
else if (/^-[IDU]./.test(a))
|
|
70
|
+
out.push(a.startsWith('-I') ? `-I${path.resolve(e.directory, a.slice(2))}` : a);
|
|
71
|
+
else if (/^-(std=|f(un)?signed-char|m32|m64)/.test(a))
|
|
72
|
+
out.push(a);
|
|
73
|
+
}
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
76
|
+
// Where each node is: clang writes a location's file and line only where they change from the last one it wrote, in
|
|
77
|
+
// the dump's order (an include's chain, `includedFrom`, doesn't count): read in that order, each node gets its own.
|
|
78
|
+
function locate(root) {
|
|
79
|
+
let file = '', line = 0;
|
|
80
|
+
const at = (l) => {
|
|
81
|
+
if (!l)
|
|
82
|
+
return undefined;
|
|
83
|
+
if (l.spellingLoc || l.expansionLoc) {
|
|
84
|
+
at(l.spellingLoc);
|
|
85
|
+
return at(l.expansionLoc);
|
|
86
|
+
}
|
|
87
|
+
if (l.file !== undefined)
|
|
88
|
+
file = l.file;
|
|
89
|
+
if (l.line !== undefined)
|
|
90
|
+
line = l.line;
|
|
91
|
+
return l.offset === undefined ? undefined : { file, line, col: l.col ?? 0, off: l.offset, tok: l.tokLen ?? 0 };
|
|
92
|
+
};
|
|
93
|
+
const walk = (n) => {
|
|
94
|
+
for (const k of Object.keys(n)) {
|
|
95
|
+
if (k === 'loc')
|
|
96
|
+
n.$at = at(n.loc);
|
|
97
|
+
else if (k === 'range') {
|
|
98
|
+
n.$b = at(n.range.begin);
|
|
99
|
+
n.$e = at(n.range.end);
|
|
100
|
+
}
|
|
101
|
+
else if (k === 'inner')
|
|
102
|
+
for (const c of n.inner)
|
|
103
|
+
walk(c);
|
|
104
|
+
}
|
|
105
|
+
n.$at ??= n.$b;
|
|
106
|
+
};
|
|
107
|
+
walk(root);
|
|
108
|
+
}
|
|
109
|
+
const CAST = new Set(['ImplicitCastExpr', 'CStyleCastExpr', 'CXXStaticCastExpr', 'CXXFunctionalCastExpr']);
|
|
110
|
+
const CMP = new Set(['==', '!=', '<', '<=', '>', '>=']), ARITH = new Set(['+', '-', '*', '/', '%', '<<', '>>', '&', '|', '^']);
|
|
111
|
+
const FAIL = { abort: 'abort()', __assert_rtn: 'an assertion failing', __assert_fail: 'an assertion failing', __assert: 'an assertion failing', _assert: 'an assertion failing', __builtin_trap: 'a trap', __builtin_unreachable: 'unreachable code reached (undefined behavior)' };
|
|
112
|
+
// --- the reading --------------------------------------------------------------------------------------------------------
|
|
113
|
+
// (`root`: the project's own files, whose functions are read too: a static inline helper of its header; by default the
|
|
114
|
+
// file's directory)
|
|
115
|
+
// (`picks`: an overloaded method's arity, the one read: `functions: ['throttle/1']`)
|
|
116
|
+
export function readC(file, entry, tu, target, cpp = false, root, picks = {}) {
|
|
117
|
+
const path = node('node:path'), fs = node('node:fs');
|
|
118
|
+
const main = path.resolve(file), base = path.basename(file), dir = `${path.resolve(root ?? main.replace(/\/[^/]*$/, ''))}/`, srcs = new Map();
|
|
119
|
+
locate(tu);
|
|
120
|
+
const top = tu.inner ?? [], mine = (n) => n.$at?.file === main || n.$at?.file === file, own = (n) => mine(n) || (!!n.$at?.file && path.resolve(n.$at.file).startsWith(dir));
|
|
121
|
+
const refuse = (n, what) => { throw new Refusal(pos(n), what); };
|
|
122
|
+
const pos = (n) => (n?.$at && own(n) ? `${path.basename(n.$at.file)}:${n.$at.line}:${n.$at.col}` : base);
|
|
123
|
+
const src = (f) => { let s = srcs.get(f); if (s === undefined) {
|
|
124
|
+
try {
|
|
125
|
+
s = fs.readFileSync(path.resolve(f), 'utf8');
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
s = '';
|
|
129
|
+
}
|
|
130
|
+
;
|
|
131
|
+
srcs.set(f, s);
|
|
132
|
+
} return s; };
|
|
133
|
+
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
|
+
// The declarations: typedefs, records and enums of every file (a header's types are the file's too), the globals.
|
|
135
|
+
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();
|
|
138
|
+
const declOf = (n) => n.decl ?? (n.inner ?? []).map(declOf).find(Boolean);
|
|
139
|
+
const visitDecl = (n) => {
|
|
140
|
+
if (n.kind === 'TypedefDecl' && n.name) {
|
|
141
|
+
const d = declOf(n);
|
|
142
|
+
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
|
+
if (d && !recName.has(d.id))
|
|
144
|
+
recName.set(d.id, n.name);
|
|
145
|
+
}
|
|
146
|
+
else if ((n.kind === 'RecordDecl' || n.kind === 'CXXRecordDecl') && n.completeDefinition) {
|
|
147
|
+
records.set(n.id, n);
|
|
148
|
+
if (n.name) {
|
|
149
|
+
tags.set(`${n.tagUsed} ${n.name}`, n.id);
|
|
150
|
+
if (cpp)
|
|
151
|
+
tags.set(n.name, n.id);
|
|
152
|
+
}
|
|
153
|
+
if (n.$at)
|
|
154
|
+
anon.set(`${n.$at.line}:${n.$at.col}`, n.id);
|
|
155
|
+
let i = 0, access = n.tagUsed === 'class' ? 'private' : 'public';
|
|
156
|
+
for (const f of n.inner ?? []) {
|
|
157
|
+
if (f.kind === 'FieldDecl') {
|
|
158
|
+
fields.set(f.id, { rec: n.id, i: i++ });
|
|
159
|
+
fieldNode.set(f.id, f);
|
|
160
|
+
}
|
|
161
|
+
else if (f.kind === 'AccessSpecDecl')
|
|
162
|
+
access = String(f.access);
|
|
163
|
+
else if (/^CXX(Method|Constructor|Destructor|Conversion)Decl$/.test(f.kind)) {
|
|
164
|
+
f.$rec = n.id;
|
|
165
|
+
f.$access = access;
|
|
166
|
+
members.set(f.id, f);
|
|
167
|
+
if (f.inner?.some(c => c.kind === 'CompoundStmt'))
|
|
168
|
+
cxx.push(f);
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
if (f.kind === 'EnumDecl' && n.name)
|
|
172
|
+
f.$outer = n.name;
|
|
173
|
+
visitDecl(f);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
else if (n.kind === 'EnumDecl') {
|
|
178
|
+
if (n.name)
|
|
179
|
+
for (const q of [n.name, ...(n.$outer ? [`${n.$outer}::${n.name}`] : []), `enum ${n.name}`])
|
|
180
|
+
enums.set(q, n);
|
|
181
|
+
let v = -1;
|
|
182
|
+
for (const c of n.inner ?? [])
|
|
183
|
+
if (c.kind === 'EnumConstantDecl') {
|
|
184
|
+
const k = c.inner?.length ? fold(c.inner[0]) : undefined;
|
|
185
|
+
v = k ?? v + 1;
|
|
186
|
+
enumConst.set(c.id, v);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
else if (n.kind === 'VarDecl')
|
|
190
|
+
globals.set(n.id, n);
|
|
191
|
+
else if (n.kind === 'LinkageSpecDecl' || n.kind === 'NamespaceDecl')
|
|
192
|
+
for (const c of n.inner ?? [])
|
|
193
|
+
visitDecl(c);
|
|
194
|
+
// (a method defined outside its class: its class by the declaration's context)
|
|
195
|
+
else if (/^CXX(Method|Constructor|Destructor)Decl$/.test(n.kind) && n.inner?.some(c => c.kind === 'CompoundStmt')) {
|
|
196
|
+
n.$rec = n.parentDeclContextId;
|
|
197
|
+
n.$access = members.get(n.previousDecl)?.$access ?? 'public';
|
|
198
|
+
cxx.push(n);
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
// A constant's value, where the node is one (literals, enum constants, a const global's, folded).
|
|
202
|
+
const fold = (n) => {
|
|
203
|
+
switch (n.kind) {
|
|
204
|
+
case 'IntegerLiteral':
|
|
205
|
+
case 'CharacterLiteral': return Number(n.value);
|
|
206
|
+
case 'CXXBoolLiteralExpr': return n.value ? 1 : 0;
|
|
207
|
+
case 'CallExpr': {
|
|
208
|
+
const m = /numeric_limits<\s*(.+?)\s*>::(max|min|lowest)\)?\s*\(\s*\)$/.exec(text(n)), it = m ? T(parseT(m[1])) : undefined;
|
|
209
|
+
if (!it || it.k !== 'int')
|
|
210
|
+
return undefined;
|
|
211
|
+
const hi = it.signed ? 2 ** (it.bits - 1) - 1 : 2 ** it.bits - 1;
|
|
212
|
+
return m[2] === 'max' ? hi : it.signed ? -hi - 1 : 0;
|
|
213
|
+
}
|
|
214
|
+
case 'ConstantExpr': return n.value !== undefined ? Number(n.value) : fold(n.inner[0]);
|
|
215
|
+
case 'ParenExpr': return fold(n.inner[0]);
|
|
216
|
+
case 'ImplicitCastExpr':
|
|
217
|
+
case 'CStyleCastExpr':
|
|
218
|
+
case 'CXXStaticCastExpr':
|
|
219
|
+
case 'CXXFunctionalCastExpr': {
|
|
220
|
+
const v = fold(n.inner[0]), t = n.type ? T(tyOf(n.type)) : undefined;
|
|
221
|
+
if (v === undefined || t?.k !== 'int')
|
|
222
|
+
return n.castKind === 'IntegralToBoolean' && v !== undefined ? +(v !== 0) : undefined;
|
|
223
|
+
// (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;
|
|
225
|
+
return Number(t.signed && w >= m / 2n ? w - m : w);
|
|
226
|
+
}
|
|
227
|
+
case 'UnaryOperator': {
|
|
228
|
+
const v = fold(n.inner[0]);
|
|
229
|
+
return v === undefined ? undefined : n.opcode === '-' ? -v : n.opcode === '+' ? v : n.opcode === '!' ? +(v === 0) : undefined;
|
|
230
|
+
}
|
|
231
|
+
case 'BinaryOperator': {
|
|
232
|
+
const a = fold(n.inner[0]), b = fold(n.inner[1]);
|
|
233
|
+
if (a === undefined || b === undefined)
|
|
234
|
+
return undefined;
|
|
235
|
+
switch (n.opcode ?? '') {
|
|
236
|
+
case '+': return a + b;
|
|
237
|
+
case '-': return a - b;
|
|
238
|
+
case '*': return a * b;
|
|
239
|
+
case '/': return b ? Math.trunc(a / b) : undefined;
|
|
240
|
+
case '%': return b ? a % b : undefined;
|
|
241
|
+
case '<<': return a * 2 ** b;
|
|
242
|
+
default: return undefined;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
case 'DeclRefExpr': {
|
|
246
|
+
const d = n.referencedDecl;
|
|
247
|
+
if (d.kind === 'EnumConstantDecl')
|
|
248
|
+
return enumConst.get(d.id);
|
|
249
|
+
const g = globals.get(d.id);
|
|
250
|
+
return g && /^const /.test(g.type.qualType) && g.inner?.length ? fold(g.inner[0]) : undefined;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return undefined;
|
|
254
|
+
};
|
|
255
|
+
// --- types: the table Go's lowering reads (TT), from clang's spelled types ---------------------------------------------
|
|
256
|
+
const ttab = [], tkey = new Map();
|
|
257
|
+
const T = (t) => ttab[t];
|
|
258
|
+
const add = (key, t) => { const k = tkey.get(key); if (k !== undefined)
|
|
259
|
+
return k; ttab.push(t); tkey.set(key, ttab.length - 1); return ttab.length - 1; };
|
|
260
|
+
const ints = {
|
|
261
|
+
char: [8, !target.charUnsigned], 'signed char': [8, true], 'unsigned char': [8, false], short: [target.short, true], 'short int': [target.short, true], 'unsigned short': [target.short, false], 'unsigned short int': [target.short, false],
|
|
262
|
+
int: [target.int, true], signed: [target.int, true], 'signed int': [target.int, true], unsigned: [target.int, false], 'unsigned int': [target.int, false],
|
|
263
|
+
long: [target.long, true], 'long int': [target.long, true], 'unsigned long': [target.long, false], 'unsigned long int': [target.long, false],
|
|
264
|
+
'long long': [target.llong, true], 'long long int': [target.llong, true], 'unsigned long long': [target.llong, false], 'unsigned long long int': [target.llong, false],
|
|
265
|
+
wchar_t: [32, true], char8_t: [8, false], char16_t: [16, false], char32_t: [32, false],
|
|
266
|
+
// (the standard's fixed widths, for a dump that holds no typedefs: C++'s, filtered)
|
|
267
|
+
int8_t: [8, true], int16_t: [16, true], int32_t: [32, true], int64_t: [64, true], uint8_t: [8, false], uint16_t: [16, false], uint32_t: [32, false], uint64_t: [64, false],
|
|
268
|
+
size_t: [target.long, false], ssize_t: [target.long, true], ptrdiff_t: [target.long, true], intptr_t: [target.long, true], uintptr_t: [target.long, false],
|
|
269
|
+
};
|
|
270
|
+
const intT = (go, bits, signed) => add(`int:${go}`, { k: 'int', go, bits, signed, ...(signed ? { ub: true } : {}) });
|
|
271
|
+
const boolT = add('bool', { k: 'bool', go: 'bool' });
|
|
272
|
+
const recT = (id, spelled) => {
|
|
273
|
+
const r = records.get(id);
|
|
274
|
+
if (!r)
|
|
275
|
+
return add(`opaque:${spelled}`, { k: 'opaque', go: spelled });
|
|
276
|
+
const go = recName.get(id) ?? (r.name ? (cpp ? r.name : `${r.tagUsed} ${r.name}`) : spelled), key = `rec:${id}`;
|
|
277
|
+
if (tkey.has(key))
|
|
278
|
+
return tkey.get(key);
|
|
279
|
+
if (r.tagUsed === 'union')
|
|
280
|
+
return add(key, { k: 'union', go });
|
|
281
|
+
const t = add(key, { k: 'struct', own: true, name: go, go, fields: [] });
|
|
282
|
+
ttab[t].fields = (r.inner ?? []).filter(f => f.kind === 'FieldDecl').map(f => ({ name: f.name ?? '', t: tyOf(f.type), embedded: false }));
|
|
283
|
+
return t;
|
|
284
|
+
};
|
|
285
|
+
function tyOf(ty) { return parseT(ty.qualType, ty.desugaredQualType); }
|
|
286
|
+
function parseT(s0, desug) {
|
|
287
|
+
const s = s0.replace(/\b(const|volatile|restrict|__restrict)\b/g, '').replace(/\s+/g, ' ').replace(/ \*/g, '*').replace(/\*/g, ' *').replace(/\* \*/g, '**').trim();
|
|
288
|
+
if (s === '_Bool' || s === 'bool')
|
|
289
|
+
return boolT;
|
|
290
|
+
const arr = /^(.*?)\s*\[(\d+)\]$/.exec(s);
|
|
291
|
+
if (arr)
|
|
292
|
+
return add(`arr:${s}`, { k: 'array', go: s, elem: parseT(arr[1]), len: Number(arr[2]), fixed: true });
|
|
293
|
+
if (/\(\*\)|\(.*\)$/.test(s))
|
|
294
|
+
return add(`fn:${s}`, { k: 'func', go: s });
|
|
295
|
+
if (/\*$/.test(s)) {
|
|
296
|
+
const e = parseT(s.replace(/\s*\*$/, ''));
|
|
297
|
+
return add(`ptr:${e}`, { k: 'ptr', go: s, elem: e });
|
|
298
|
+
}
|
|
299
|
+
if (/&$/.test(s))
|
|
300
|
+
return add(`ref:${s}`, { k: 'ref', go: s });
|
|
301
|
+
if (ints[s])
|
|
302
|
+
return intT(s, ...ints[s]);
|
|
303
|
+
if (cpp && /^std::\w+$/.test(s) && ints[s.slice(5)])
|
|
304
|
+
return intT(s.slice(5), ...ints[s.slice(5)]);
|
|
305
|
+
if (s === 'float' || s === 'double' || s === 'long double')
|
|
306
|
+
return add(`f:${s}`, { k: 'float', go: s });
|
|
307
|
+
if (s === 'void')
|
|
308
|
+
return add('void', { k: 'void', go: 'void' });
|
|
309
|
+
const un = /^(struct|union|class) \((?:unnamed|anonymous) (?:struct|union|class) at .*:(\d+):(\d+)\)$/.exec(s);
|
|
310
|
+
if (un) {
|
|
311
|
+
const id = anon.get(`${un[2]}:${un[3]}`);
|
|
312
|
+
return id ? recT(id) : add(`opaque:${s}`, { k: 'opaque', go: s });
|
|
313
|
+
}
|
|
314
|
+
const std = /^std::(?:__1::|__cxx11::)?(array<(.+), (\d+)>|mutex|recursive_mutex|timed_mutex|shared_mutex)$/.exec(s);
|
|
315
|
+
if (std)
|
|
316
|
+
return std[2] ? add(`arr:${s}`, { k: 'array', go: s, elem: parseT(std[2]), len: Number(std[3]), fixed: true }) : add(`mutex:${s}`, { k: 'ext', mutex: true, go: s });
|
|
317
|
+
if (enums.has(s))
|
|
318
|
+
return enumT(s);
|
|
319
|
+
if (tags.has(s))
|
|
320
|
+
return recT(tags.get(s), s);
|
|
321
|
+
// (C++: a class named with its namespaces, as `this`'s type is)
|
|
322
|
+
if (cpp && /::/.test(s) && tags.has(s.replace(/^(\w+::)+/, '')))
|
|
323
|
+
return recT(tags.get(s.replace(/^(\w+::)+/, '')), s);
|
|
324
|
+
const td = typedefs.get(s.replace(/^(struct|union|class) /, ''));
|
|
325
|
+
if (td) {
|
|
326
|
+
if (td.rec && records.has(td.rec))
|
|
327
|
+
return recT(td.rec, s);
|
|
328
|
+
const inner = parseT(td.desug ?? td.qual), it = T(inner);
|
|
329
|
+
// (an integer type keeps its name: the notes and the harness spell it so)
|
|
330
|
+
return it.k === 'int' ? intT(s, it.bits, it.signed) : inner;
|
|
331
|
+
}
|
|
332
|
+
// (an integer typedef the dump doesn't hold, already met desugared: a field's)
|
|
333
|
+
if (tkey.has(`int:${s}`))
|
|
334
|
+
return tkey.get(`int:${s}`);
|
|
335
|
+
if (desug && desug !== s0) {
|
|
336
|
+
const it = parseT(desug);
|
|
337
|
+
return T(it).k === 'int' && !/[<:]/.test(s) ? intT(s, T(it).bits, T(it).signed) : it;
|
|
338
|
+
}
|
|
339
|
+
return add(`opaque:${s}`, { k: 'opaque', go: s });
|
|
340
|
+
}
|
|
341
|
+
// An enum is its integer type: its fixed one (C++), else int, unsigned where no enumerator is negative (clang's).
|
|
342
|
+
const enumT = (s) => {
|
|
343
|
+
const d = enums.get(s), vals = (d?.inner ?? []).filter(c => c.kind === 'EnumConstantDecl').map(c => enumConst.get(c.id) ?? 0);
|
|
344
|
+
const fixed = d?.fixedUnderlyingType?.qualType, ft = fixed ? T(parseT(fixed)) : undefined;
|
|
345
|
+
return intT(s, ft?.bits ?? target.int, ft ? ft.signed : vals.some(v => v < 0));
|
|
346
|
+
};
|
|
347
|
+
for (const n of top)
|
|
348
|
+
if (!n.isImplicit)
|
|
349
|
+
visitDecl(n);
|
|
350
|
+
// (a typedef of a struct declared before its definition names the definition)
|
|
351
|
+
for (const [name, td] of typedefs) {
|
|
352
|
+
const id = tags.get(td.qual.replace(/^const /, ''));
|
|
353
|
+
if (id && !recName.has(id))
|
|
354
|
+
recName.set(id, name);
|
|
355
|
+
if (id && !(td.rec && records.has(td.rec)))
|
|
356
|
+
td.rec = id;
|
|
357
|
+
}
|
|
358
|
+
// --- expressions ---------------------------------------------------------------------------------------------------------
|
|
359
|
+
const funcs = new Map();
|
|
360
|
+
for (const n of top)
|
|
361
|
+
if ((n.kind === 'FunctionDecl') && own(n) && n.inner?.some(c => c.kind === 'CompoundStmt'))
|
|
362
|
+
funcs.set(n.name, n);
|
|
363
|
+
const entryRec = tags.get(`struct ${entry}`) ?? tags.get(entry) ?? typedefs.get(entry)?.rec;
|
|
364
|
+
const params = (f) => (f.inner ?? []).filter(c => c.kind === 'ParmVarDecl');
|
|
365
|
+
// C++: the class's methods (this the receiver; an overload refused where it is called), its constructor.
|
|
366
|
+
const ctors = [], overloaded = new Set();
|
|
367
|
+
for (const m of cxx) {
|
|
368
|
+
if (m.$rec !== entryRec)
|
|
369
|
+
continue;
|
|
370
|
+
if (m.kind === 'CXXConstructorDecl') {
|
|
371
|
+
if (!/\((const )?\w+ &&?\)/.test(m.type.qualType) || params(m).length !== 1)
|
|
372
|
+
ctors.push(m);
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
if (m.kind !== 'CXXMethodDecl' || /^operator/.test(m.name))
|
|
376
|
+
continue;
|
|
377
|
+
m.$m = true;
|
|
378
|
+
if (picks[m.name] !== undefined) {
|
|
379
|
+
if (params(m).length === picks[m.name])
|
|
380
|
+
funcs.set(m.name, m);
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
383
|
+
if (funcs.has(m.name))
|
|
384
|
+
overloaded.add(m.name);
|
|
385
|
+
funcs.set(m.name, m);
|
|
386
|
+
}
|
|
387
|
+
const entryT = entryRec ? recT(entryRec, entry) : refuse(undefined, `no ${cpp ? 'class' : 'struct'} ${entry} in ${base}${cpp ? ' (a class template is outside the subset)' : ''}`);
|
|
388
|
+
// An out-parameter (a pointer to an integer or a bool, not the receiver): a pointer to a box of one field, `value`,
|
|
389
|
+
// *p its field.
|
|
390
|
+
const boxed = new Map(), byValue = new Map(), refs = new Set();
|
|
391
|
+
const boxOf = (q) => {
|
|
392
|
+
// (a const reference to an integer is its value: an input)
|
|
393
|
+
if (/^const [^*&]*&$/.test(q.type.qualType.trim())) {
|
|
394
|
+
const v = parseT(q.type.qualType.trim().slice(0, -1), q.type.desugaredQualType?.trim().replace(/&$/, ''));
|
|
395
|
+
byValue.set(q.id, v);
|
|
396
|
+
return v;
|
|
397
|
+
}
|
|
398
|
+
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);
|
|
399
|
+
if (pt.k !== 'ptr' || !['int', 'bool'].includes(T(pt.elem).k))
|
|
400
|
+
return tyOf(q.type);
|
|
401
|
+
if (ref)
|
|
402
|
+
refs.add(q.id);
|
|
403
|
+
const e = pt.elem, b = add(`box:${e}`, { k: 'struct', own: true, box: true, name: T(e).go, go: T(e).go, fields: [{ name: 'value', t: e, embedded: false }] }), bp = add(`ptr:${b}`, { k: 'ptr', go: pt.go, elem: b });
|
|
404
|
+
boxed.set(q.id, bp);
|
|
405
|
+
return bp;
|
|
406
|
+
};
|
|
407
|
+
const THIS = { kind: 'ParmVarDecl', id: 'this', name: 'this', type: { qualType: `${entry} *` } };
|
|
408
|
+
const recvOf = (f) => { if (f.$m)
|
|
409
|
+
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));
|
|
411
|
+
for (const f of [...funcs.values(), ...ctors])
|
|
412
|
+
for (const q of args0(f))
|
|
413
|
+
boxOf(q);
|
|
414
|
+
const strip = (n) => ((['ParenExpr', 'MaterializeTemporaryExpr', 'ExprWithCleanups', 'CXXBindTemporaryExpr', 'CXXDefaultInitExpr'].includes(n.kind) || (n.kind === 'UnaryOperator' && n.opcode === '__extension__') || (CAST.has(n.kind) && ['LValueToRValue', 'NoOp'].includes(n.castKind))) && n.inner?.length ? strip(n.inner[0]) : n);
|
|
415
|
+
// A default member initializer's use (CXXDefaultInitExpr): clang 19 dumps the initializer inside it, clang 18 and
|
|
416
|
+
// earlier nothing (nor the field), so a constructor's is given its field's own (an empty one elsewhere is refused).
|
|
417
|
+
const fillDefault = (n, f) => {
|
|
418
|
+
if (n.kind === 'CXXDefaultInitExpr' && !n.inner?.length && f?.inner?.length && !f.inner[0].kind.endsWith('Attr'))
|
|
419
|
+
n.inner = [f.inner[0]];
|
|
420
|
+
return n;
|
|
421
|
+
};
|
|
422
|
+
const at = (n, t) => ({ p: pos(n), t, text: text(n) });
|
|
423
|
+
const no = (n, why) => ({ e: 'no', ...at(n, -1), why });
|
|
424
|
+
const lit = (n, v, t) => ({ e: 'lit', ...at(n, t), c: { int: String(v) } });
|
|
425
|
+
const isBoolish = (n0) => {
|
|
426
|
+
const n = strip(n0);
|
|
427
|
+
if (n.kind === 'BinaryOperator' && (CMP.has(n.opcode) || n.opcode === '&&' || n.opcode === '||'))
|
|
428
|
+
return true;
|
|
429
|
+
if (n.kind === 'UnaryOperator' && n.opcode === '!')
|
|
430
|
+
return true;
|
|
431
|
+
if ((n.kind === 'ImplicitCastExpr' || n.kind === 'CStyleCastExpr') && n.castKind === 'IntegralCast')
|
|
432
|
+
return isBoolish(n.inner[0]);
|
|
433
|
+
if (n.kind === 'CallExpr' && calleeName(n) === '__builtin_expect')
|
|
434
|
+
return isBoolish(n.inner[1]);
|
|
435
|
+
return n.type ? T(tyOf(n.type)).k === 'bool' : false;
|
|
436
|
+
};
|
|
437
|
+
// (a sum or product of promoted unsigned values and constants ≥ 0: at least 0, by its form)
|
|
438
|
+
const nonneg = (n0) => {
|
|
439
|
+
const n = strip(n0), k = fold(n);
|
|
440
|
+
if (k !== undefined)
|
|
441
|
+
return k >= 0;
|
|
442
|
+
if (n.kind === 'ImplicitCastExpr' && n.castKind === 'IntegralCast')
|
|
443
|
+
return T(tyOf(n.inner[0].type)).signed === false;
|
|
444
|
+
if (n.kind === 'BinaryOperator' && (n.opcode === '+' || n.opcode === '*'))
|
|
445
|
+
return nonneg(n.inner[0]) && nonneg(n.inner[1]);
|
|
446
|
+
return !!n.type && T(tyOf(n.type)).signed === false;
|
|
447
|
+
};
|
|
448
|
+
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; };
|
|
449
|
+
// A condition: a boolean as it is; a number compared with 0; a pointer with NULL.
|
|
450
|
+
const cond = (n0) => {
|
|
451
|
+
const n = strip(n0);
|
|
452
|
+
if (n.kind === 'CallExpr' && calleeName(n) === '__builtin_expect')
|
|
453
|
+
return cond(n.inner[1]);
|
|
454
|
+
if ((n.kind === 'ImplicitCastExpr' || n.kind === 'CStyleCastExpr') && ['IntegralCast', 'IntegralToBoolean', 'PointerToBoolean'].includes(n.castKind) && isBoolish(n.inner[0]))
|
|
455
|
+
return cond(n.inner[0]);
|
|
456
|
+
if (n.kind === 'BinaryOperator' && CMP.has(n.opcode))
|
|
457
|
+
return { e: 'bin', ...at(n, boolT), op: n.opcode, x: x(n.inner[0]), y: x(n.inner[1]) };
|
|
458
|
+
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]) };
|
|
460
|
+
if (n.kind === 'UnaryOperator' && n.opcode === '!')
|
|
461
|
+
return { e: 'un', ...at(n, boolT), op: '!', x: cond(n.inner[0]) };
|
|
462
|
+
const v = (n.kind === 'ImplicitCastExpr' && (n.castKind === 'IntegralToBoolean' || n.castKind === 'PointerToBoolean')) ? n.inner[0] : n, vt = v.type ? tyOf(v.type) : -1;
|
|
463
|
+
if (vt >= 0 && T(vt).k === 'bool')
|
|
464
|
+
return x(v);
|
|
465
|
+
const k = fold(v);
|
|
466
|
+
if (k !== undefined)
|
|
467
|
+
return { e: 'lit', ...at(n, boolT), c: { bool: k !== 0 } };
|
|
468
|
+
if (vt >= 0 && T(vt).k === 'ptr')
|
|
469
|
+
return { e: 'bin', ...at(n, boolT), op: '!=', x: x(v), y: { e: 'id', ...at(n, vt), o: 'nil' } };
|
|
470
|
+
if (vt >= 0 && T(vt).k === 'int')
|
|
471
|
+
return { e: 'bin', ...at(n, boolT), op: '!=', x: x(v), y: lit(n, 0, vt) };
|
|
472
|
+
return no(n, `${text(n)}: a condition of type ${n.type?.qualType}`);
|
|
473
|
+
};
|
|
474
|
+
const x = (n0) => {
|
|
475
|
+
const n = strip(n0), t = n.type ? tyOf(n.type) : -1, a = at(n, t), kids = n.inner ?? [];
|
|
476
|
+
const k = t >= 0 && T(t).k === 'int' ? fold(n) : undefined;
|
|
477
|
+
if (k !== undefined)
|
|
478
|
+
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)`);
|
|
479
|
+
switch (n.kind) {
|
|
480
|
+
case 'CXXBoolLiteralExpr': return { e: 'lit', ...a, c: { bool: !!n.value } };
|
|
481
|
+
case 'CXXThisExpr': return { e: 'id', ...a, o: 'var', id: 'this', n: 'this' };
|
|
482
|
+
case 'CXXNullPtrLiteralExpr': return { e: 'id', ...a, o: 'nil' };
|
|
483
|
+
case 'CXXThrowExpr': return { e: 'call', ...a, builtin: 'panic', a: [], what: 'an exception thrown' };
|
|
484
|
+
case 'CXXMemberCallExpr': return memberCall(n);
|
|
485
|
+
case 'CXXOperatorCallExpr': {
|
|
486
|
+
// std::array's [] (its other operators, and every other class's, are other code)
|
|
487
|
+
const o = strip(kids[1] ?? n), ot = o.type ? T(tyOf(o.type)) : undefined;
|
|
488
|
+
if (strip(strip(kids[0]).inner?.[0] ?? kids[0]).referencedDecl?.name === 'operator[]' && ot?.fixed)
|
|
489
|
+
return { e: 'index', ...a, x: place(o), i: x(kids[2]) };
|
|
490
|
+
return no(n, `${text(n)}: an overloaded operator (other code)`);
|
|
491
|
+
}
|
|
492
|
+
case 'DeclRefExpr': {
|
|
493
|
+
const d = n.referencedDecl;
|
|
494
|
+
if (d.kind === 'ParmVarDecl') {
|
|
495
|
+
const v = { e: 'id', ...a, ...(boxed.has(d.id) ? { t: boxed.get(d.id) } : {}), o: 'var', id: d.id, n: d.name };
|
|
496
|
+
return refs.has(d.id) ? { e: 'sel', ...a, k: 'field', x: v, path: [0] } : v;
|
|
497
|
+
}
|
|
498
|
+
if (d.kind === 'VarDecl' && /&$/.test(d.type?.qualType ?? ''))
|
|
499
|
+
return no(n, `${d.name}: a reference held in a local (a second name for a place)`);
|
|
500
|
+
if (d.kind === 'VarDecl')
|
|
501
|
+
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
|
+
if (d.kind === 'FunctionDecl')
|
|
503
|
+
return no(n, `${d.name}: a function used as a value`);
|
|
504
|
+
return no(n, `${text(n)}: a ${d.kind}`);
|
|
505
|
+
}
|
|
506
|
+
case 'MemberExpr': {
|
|
507
|
+
const f = fields.get(n.referencedMemberDecl);
|
|
508
|
+
if (!f)
|
|
509
|
+
return no(n, `${text(n)}: a member that isn't a field of a struct read here`);
|
|
510
|
+
if (records.get(f.rec).tagUsed === 'union')
|
|
511
|
+
return no(n, `${text(n)}: a union's member (one storage read as several types)`);
|
|
512
|
+
const b = kids[0];
|
|
513
|
+
return { e: 'sel', ...a, k: 'field', x: n.isArrow ? x(b) : place(b), path: [f.i] };
|
|
514
|
+
}
|
|
515
|
+
case 'ImplicitCastExpr':
|
|
516
|
+
case 'CStyleCastExpr':
|
|
517
|
+
case 'CXXStaticCastExpr':
|
|
518
|
+
case 'CXXFunctionalCastExpr': {
|
|
519
|
+
const ck = n.castKind, inner = kids[0];
|
|
520
|
+
if (ck === 'IntegralCast') {
|
|
521
|
+
const it = tyOf(inner.type);
|
|
522
|
+
if (T(it).k === 'bool')
|
|
523
|
+
return { e: 'ite', ...a, x: cond(inner), y: lit(n, 1, t), b: lit(n, 0, t) };
|
|
524
|
+
return it === t ? x(inner) : { e: 'call', ...a, conv: true, a: [x(inner)] };
|
|
525
|
+
}
|
|
526
|
+
if (ck === 'IntegralToBoolean' || ck === 'PointerToBoolean')
|
|
527
|
+
return cond(n);
|
|
528
|
+
if (ck === 'NullToPointer')
|
|
529
|
+
return { e: 'id', ...a, o: 'nil' };
|
|
530
|
+
if (ck === 'ToVoid')
|
|
531
|
+
return x(inner);
|
|
532
|
+
if (ck === 'FunctionToPointerDecay' || ck === 'BuiltinFnToFnPtr')
|
|
533
|
+
return x(inner);
|
|
534
|
+
if (ck === 'BitCast' && T(t).k === 'ptr' && strip(inner).type && tyOf(strip(inner).type) === t)
|
|
535
|
+
return x(inner);
|
|
536
|
+
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
|
+
}
|
|
538
|
+
case 'BinaryOperator': {
|
|
539
|
+
const op = n.opcode, [l, r] = kids;
|
|
540
|
+
if (CMP.has(op) || op === '&&' || op === '||') {
|
|
541
|
+
const c = cond(n);
|
|
542
|
+
return T(t).k === 'bool' ? c : { e: 'ite', ...a, x: c, y: lit(n, 1, t), b: lit(n, 0, t) };
|
|
543
|
+
}
|
|
544
|
+
if (op === '&') {
|
|
545
|
+
// u & (2^k − 1): u's low k bits, u % 2^k for an unsigned u.
|
|
546
|
+
const m = fold(r);
|
|
547
|
+
// (of a signed x, two's complement: the floor remainder, ((x % 2^k) + 2^k) % 2^k by C's truncating %)
|
|
548
|
+
if (m !== undefined && m > 0 && Number.isInteger(Math.log2(m + 1))) {
|
|
549
|
+
const k = lit(r, m + 1, t), lo = { e: 'bin', ...a, op: '%', x: x(l), y: k };
|
|
550
|
+
return T(t).signed === false || nonneg(l) ? lo : { e: 'bin', ...a, op: '%', x: { e: 'bin', ...a, op: '+', x: lo, y: k }, y: k };
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
if (ARITH.has(op))
|
|
554
|
+
return { e: 'bin', ...a, op: op === '^' ? '^' : op, x: x(l), y: x(r) };
|
|
555
|
+
if (op === ',')
|
|
556
|
+
return no(n, 'the comma operator');
|
|
557
|
+
return no(n, `${text(n)}: an assignment inside an expression (the subset reads it as a statement)`);
|
|
558
|
+
}
|
|
559
|
+
case 'UnaryOperator': {
|
|
560
|
+
const op = n.opcode;
|
|
561
|
+
if (op === '!')
|
|
562
|
+
return T(t).k === 'bool' ? cond(n) : { e: 'ite', ...a, x: cond(n), y: lit(n, 1, t), b: lit(n, 0, t) };
|
|
563
|
+
if (op === '-' || op === '+')
|
|
564
|
+
return { e: 'un', ...a, op, x: x(kids[0]) };
|
|
565
|
+
if (op === '~')
|
|
566
|
+
return { e: 'un', ...a, op: '^', x: x(kids[0]) };
|
|
567
|
+
if (op === '&')
|
|
568
|
+
return { e: 'un', ...a, op: '&', x: place(kids[0]) };
|
|
569
|
+
if (op === '*') {
|
|
570
|
+
const r = strip(kids[0]);
|
|
571
|
+
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
|
+
}
|
|
573
|
+
return no(n, `${text(n)}: ${op} inside an expression (the subset reads it as a statement)`);
|
|
574
|
+
}
|
|
575
|
+
case 'ConditionalOperator': return { e: 'ite', ...a, x: cond(kids[0]), y: x(kids[1]), b: x(kids[2]) };
|
|
576
|
+
case 'CallExpr': return call(n);
|
|
577
|
+
case 'CompoundLiteralExpr': return x(kids[0]);
|
|
578
|
+
case 'InitListExpr': {
|
|
579
|
+
// A struct's initializer: clang's semantic form lists every field, one not given an implicit zero.
|
|
580
|
+
const tt = T(t);
|
|
581
|
+
// (a scalar's braces, C++'s {v} and {})
|
|
582
|
+
if ((tt.k === 'int' || tt.k === 'bool') && kids.length <= 1)
|
|
583
|
+
return kids.length ? x(kids[0]) : lit(n, 0, t);
|
|
584
|
+
if (tt.k !== 'struct')
|
|
585
|
+
return no(n, `${text(n)}: an initializer of a ${n.type.qualType}`);
|
|
586
|
+
return { e: 'comp', ...a, elts: kids.flatMap((k, i) => (k.kind === 'ImplicitValueInitExpr' ? [] : [{ field: tt.fields[i].name, v: x(k) }])) };
|
|
587
|
+
}
|
|
588
|
+
case 'ArraySubscriptExpr': {
|
|
589
|
+
// An index into an array of the state (its decay to a pointer undone); pointer arithmetic is outside.
|
|
590
|
+
const b = strip(kids[0]), arr = b.kind === 'ImplicitCastExpr' && b.castKind === 'ArrayToPointerDecay' ? b.inner[0] : undefined;
|
|
591
|
+
return arr ? { e: 'index', ...a, x: place(arr), i: x(kids[1]) } : no(n, `${text(n)}: an index into a pointer (pointer arithmetic)`);
|
|
592
|
+
}
|
|
593
|
+
case 'StringLiteral': return no(n, `${text(n)}: a string (char arrays are outside the subset)`);
|
|
594
|
+
case 'FloatingLiteral': return no(n, `${text(n)}: a float (the subset reads integers)`);
|
|
595
|
+
case 'UnaryExprOrTypeTraitExpr': return no(n, `${text(n)}: sizeof (the target's layout)`);
|
|
596
|
+
}
|
|
597
|
+
return no(n, `${text(n)}: an expression the subset doesn't read (${n.kind})`);
|
|
598
|
+
};
|
|
599
|
+
// A place: a local, a field (through a pointer: ->), *p.
|
|
600
|
+
const place = (n0) => {
|
|
601
|
+
const n = strip(n0);
|
|
602
|
+
if (n.kind === 'DeclRefExpr' || n.kind === 'MemberExpr' || n.kind === 'CXXOperatorCallExpr' || (n.kind === 'UnaryOperator' && n.opcode === '*'))
|
|
603
|
+
return x(n);
|
|
604
|
+
if (n.kind === 'ArraySubscriptExpr')
|
|
605
|
+
return x(n);
|
|
606
|
+
return no(n, `${text(n)}: a place the subset doesn't write`);
|
|
607
|
+
};
|
|
608
|
+
const call = (n) => {
|
|
609
|
+
const t = n.type ? tyOf(n.type) : -1, a = at(n, t), name = calleeName(n), args = (n.inner ?? []).slice(1);
|
|
610
|
+
if (!name)
|
|
611
|
+
return no(n, `${text(n)}: a call through a function pointer`);
|
|
612
|
+
if (FAIL[name])
|
|
613
|
+
return { e: 'call', ...a, builtin: 'panic', a: [], what: FAIL[name] };
|
|
614
|
+
if (name === '__builtin_expect')
|
|
615
|
+
return x(args[0]);
|
|
616
|
+
if (name === 'exit' || name === '_Exit')
|
|
617
|
+
return no(n, `${name}: the process ends`);
|
|
618
|
+
if (cpp && (name === 'min' || name === 'max') && args.length === 2)
|
|
619
|
+
return { e: 'call', ...a, builtin: name, a: args.map(x) };
|
|
620
|
+
const f = funcs.get(name);
|
|
621
|
+
if (f?.$m)
|
|
622
|
+
return no(n, `${name}: a static method`);
|
|
623
|
+
// (another file's or a library's function: at the boundary where it is inferred, with its end, where the check
|
|
624
|
+
// stubs an answered one in a copy of the file)
|
|
625
|
+
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}` } : {}) };
|
|
627
|
+
if (recvOf(f) && args.length)
|
|
628
|
+
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
|
+
return { e: 'call', ...a, f: { e: 'id', ...at(n, -1), o: 'func', n: name }, a: args.map(x) };
|
|
630
|
+
};
|
|
631
|
+
// obj.m(...) and this->m(...): a method of the class, inlined with its receiver (a pointer to the object); std::array's
|
|
632
|
+
// size() its length; any other class's method is other code.
|
|
633
|
+
const memberCall = (n) => {
|
|
634
|
+
const me = strip(n.inner[0]), m = members.get(me.referencedMemberDecl ?? ''), b = me.inner?.[0], t = n.type ? tyOf(n.type) : -1;
|
|
635
|
+
const bt = b?.type ? T(tyOf(b.type)) : undefined;
|
|
636
|
+
if (me.name === 'size' && bt?.fixed)
|
|
637
|
+
return lit(n, bt.len, t);
|
|
638
|
+
if (!m || m.$rec !== entryRec || !b)
|
|
639
|
+
return no(n, `${text(n)}: a method of another class (other code)`);
|
|
640
|
+
if (overloaded.has(m.name) || (picks[m.name] !== undefined && params(m).length !== picks[m.name]))
|
|
641
|
+
return no(n, `${m.name}: an overloaded method`);
|
|
642
|
+
if (members.get(me.referencedMemberDecl)?.virtual || funcs.get(m.name)?.virtual)
|
|
643
|
+
return no(n, `${m.name}: a virtual method (its dispatch)`);
|
|
644
|
+
if (!funcs.has(m.name))
|
|
645
|
+
return no(n, `${m.name}: a method with no body here`);
|
|
646
|
+
return { e: 'call', ...at(n, t), f: { e: 'sel', ...at(n, -1), k: 'method', x: me.isArrow ? x(b) : { e: 'un', ...at(b, add(`ptr:${entryT}`, { k: 'ptr', go: `${entry} *`, elem: entryT })), op: '&', x: place(b) }, n: m.name, mtype: entry, mptr: true }, a: (n.inner ?? []).slice(1).map(x) };
|
|
647
|
+
};
|
|
648
|
+
// --- statements -------------------------------------------------------------------------------------------------------
|
|
649
|
+
const topStmts = new Set();
|
|
650
|
+
const sno = (n, why) => [{ s: 'no', p: pos(n), why }];
|
|
651
|
+
const conv = (n, to, v) => ({ e: 'call', ...at(n, to), conv: true, a: [v] });
|
|
652
|
+
const ss = (ns) => ns.flatMap(st);
|
|
653
|
+
const block = (n) => (n.kind === 'CompoundStmt' ? ss(n.inner ?? []) : st(n));
|
|
654
|
+
const ends = (b) => { const l = b.at(-1); return !!l && (l.s === 'return' || l.s === 'panic' || (l.s === 'branch' && l.tok === 'break')); };
|
|
655
|
+
function st(n) {
|
|
656
|
+
const p = pos(n), kids = n.inner ?? [];
|
|
657
|
+
switch (n.kind) {
|
|
658
|
+
case 'CompoundStmt': return [{ s: 'block', p, b: ss(kids) }];
|
|
659
|
+
case 'NullStmt': return [];
|
|
660
|
+
case 'DeclStmt': return kids.flatMap((d) => {
|
|
661
|
+
if (d.kind !== 'VarDecl')
|
|
662
|
+
return d.kind === 'RecordDecl' || d.kind === 'TypedefDecl' || d.kind === 'EnumDecl' ? [] : sno(d, `a ${d.kind} in a function`);
|
|
663
|
+
if (d.storageClass === 'static')
|
|
664
|
+
return sno(d, `${d.name}: a static local (state every call shares)`);
|
|
665
|
+
// std::lock_guard, std::scoped_lock, std::unique_lock of one of the object's mutexes, in the function's own
|
|
666
|
+
// block: Go's Lock with its deferred Unlock (the step atomic)
|
|
667
|
+
if (/^std::(?:__1::)?(lock_guard|scoped_lock|unique_lock)</.test(d.type.qualType)) {
|
|
668
|
+
const c = (d.inner ?? []).find(k => k.kind === 'CXXConstructExpr'), mu = c?.inner?.length === 1 ? strip(c.inner[0]) : undefined;
|
|
669
|
+
if (!mu || !topStmts.has(n.id) || mu.kind !== 'MemberExpr' || !T(tyOf(mu.type)).mutex)
|
|
670
|
+
return sno(d, `${d.name}: a lock of other than one mutex of the object, or inside a block (the subset makes the whole step atomic)`);
|
|
671
|
+
const lk = (op) => ({ e: 'call', ...at(d, -1), f: { e: 'sel', ...at(d, -1), k: 'method', x: x(mu), n: op }, a: [] });
|
|
672
|
+
return [{ s: 'expr', p, x: lk('Lock') }, { s: 'defer', p, x: lk('Unlock') }];
|
|
673
|
+
}
|
|
674
|
+
const init = d.init ? (d.inner ?? []).find(c => !c.kind.endsWith('Attr')) : undefined;
|
|
675
|
+
if (!init)
|
|
676
|
+
return [];
|
|
677
|
+
return [{ s: 'decl', p, specs: [{ names: [{ name: d.name, id: d.id, t: tyOf(d.type) }], v: [x(init)] }] }];
|
|
678
|
+
});
|
|
679
|
+
case 'ReturnStmt': return [{ s: 'return', p, r: kids.length ? [x(kids[0])] : [] }];
|
|
680
|
+
case 'IfStmt': {
|
|
681
|
+
if (n.hasInitStorage || n.hasVarStorage)
|
|
682
|
+
return sno(n, 'an if with a declaration');
|
|
683
|
+
const [c, th, el] = kids;
|
|
684
|
+
return [{ s: 'if', p, c: cond(c), then: block(th), ...(el ? { else: { s: 'block', p: pos(el), b: block(el) } } : {}) }];
|
|
685
|
+
}
|
|
686
|
+
case 'SwitchStmt': return switchC(n);
|
|
687
|
+
case 'BreakStmt': return [{ s: 'branch', p, tok: 'break' }];
|
|
688
|
+
case 'ContinueStmt': return sno(n, 'continue (loops are outside the subset)');
|
|
689
|
+
case 'CXXTryStmt': return sno(n, 'try and catch (an exception is the step\'s refusal)');
|
|
690
|
+
case 'ExprWithCleanups': return st(kids[0]);
|
|
691
|
+
case 'CXXForRangeStmt': return sno(n, 'a loop (for over a range)');
|
|
692
|
+
case 'WhileStmt':
|
|
693
|
+
case 'DoStmt':
|
|
694
|
+
case 'ForStmt': return sno(n, `a loop (${n.kind === 'WhileStmt' ? 'while' : n.kind === 'DoStmt' ? 'do' : 'for'})`);
|
|
695
|
+
case 'GotoStmt':
|
|
696
|
+
case 'IndirectGotoStmt': return sno(n, 'goto');
|
|
697
|
+
case 'LabelStmt': return sno(n, 'a label (goto)');
|
|
698
|
+
case 'CaseStmt':
|
|
699
|
+
case 'DefaultStmt': return sno(n, 'a case label inside a block of the switch');
|
|
700
|
+
case 'AttributedStmt': return ss(kids.filter(c => !c.kind.endsWith('Attr')));
|
|
701
|
+
case 'ParenExpr': return st(kids[0]);
|
|
702
|
+
case 'StmtExpr': return block(kids[0]);
|
|
703
|
+
case 'CStyleCastExpr':
|
|
704
|
+
case 'ImplicitCastExpr':
|
|
705
|
+
if (n.castKind === 'ToVoid') {
|
|
706
|
+
const i = strip(kids[0]);
|
|
707
|
+
return ['DeclRefExpr', 'UnaryExprOrTypeTraitExpr', 'IntegerLiteral'].includes(i.kind) ? [] : st(i);
|
|
708
|
+
}
|
|
709
|
+
break;
|
|
710
|
+
case 'ConditionalOperator': return [{ s: 'if', p, c: cond(kids[0]), then: st(kids[1]), else: { s: 'block', p, b: st(kids[2]) } }];
|
|
711
|
+
case 'BinaryOperator': {
|
|
712
|
+
if (n.opcode === ',')
|
|
713
|
+
return ss(kids);
|
|
714
|
+
if (n.opcode !== '=')
|
|
715
|
+
break;
|
|
716
|
+
const [l, r] = kids, rs = strip(r);
|
|
717
|
+
// a = b = c: b set first, then a to b's value.
|
|
718
|
+
if (rs.kind === 'BinaryOperator' && rs.opcode === '=')
|
|
719
|
+
return [...st(rs), { s: 'assign', p, op: '=', l: [place(l)], r: [conv2(r, tyOf(l.type), x(rs.inner[0]))] }];
|
|
720
|
+
return [{ s: 'assign', p, op: '=', l: [place(l)], r: [x(r)] }];
|
|
721
|
+
}
|
|
722
|
+
case 'CompoundAssignOperator': {
|
|
723
|
+
const [l, r] = kids, lt = tyOf(l.type), ct = tyOf(n.computeLHSType), op = n.opcode.slice(0, -1);
|
|
724
|
+
const m = op === '&' ? fold(r) : undefined, mask = m !== undefined && m > 0 && Number.isInteger(Math.log2(m + 1)) && T(ct).signed === false;
|
|
725
|
+
const y = mask ? lit(r, m + 1, ct) : x(r), bop = mask ? '%' : op;
|
|
726
|
+
if (ct === lt)
|
|
727
|
+
return [{ s: 'assign', p, op: `${bop}=`, l: [place(l)], r: [y] }];
|
|
728
|
+
// computed at the wider type, then converted back: x = (T)((C)x op y)
|
|
729
|
+
return [{ s: 'assign', p, op: '=', l: [place(l)], r: [conv(n, lt, { e: 'bin', ...at(n, ct), op: bop, x: conv(l, ct, x(l)), y })] }];
|
|
730
|
+
}
|
|
731
|
+
case 'UnaryOperator': {
|
|
732
|
+
if (n.opcode === '__extension__')
|
|
733
|
+
return st(kids[0]);
|
|
734
|
+
if (!['++', '--'].includes(n.opcode))
|
|
735
|
+
break;
|
|
736
|
+
const l = kids[0], lt = tyOf(l.type), tt = T(lt);
|
|
737
|
+
if (tt.k !== 'int')
|
|
738
|
+
return sno(n, `${text(n)}: ${n.opcode} of a ${l.type.qualType}`);
|
|
739
|
+
if (tt.bits >= target.int)
|
|
740
|
+
return [{ s: 'incdec', p, op: n.opcode, x: place(l) }];
|
|
741
|
+
// (a narrow integer is promoted to int, the result converted back)
|
|
742
|
+
const it = intT('int', target.int, true);
|
|
743
|
+
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
|
+
}
|
|
745
|
+
case 'CallExpr':
|
|
746
|
+
case 'CXXMemberCallExpr': return [{ s: 'expr', p, x: n.kind === 'CallExpr' ? call(n) : memberCall(n) }];
|
|
747
|
+
}
|
|
748
|
+
if (n.kind.endsWith('Expr') || n.kind.endsWith('Literal') || n.kind.endsWith('Operator'))
|
|
749
|
+
return [{ s: 'expr', p, x: x(n) }];
|
|
750
|
+
return sno(n, `a statement the subset doesn't read (${n.kind})`);
|
|
751
|
+
}
|
|
752
|
+
const conv2 = (n, to, v) => (T(to).k === 'int' && v.t !== to ? conv(n, to, v) : v);
|
|
753
|
+
// A switch: its body's labels, each a case of Go's shape; one that doesn't end in a break or a return runs into the next.
|
|
754
|
+
function switchC(n) {
|
|
755
|
+
const kids = n.inner ?? [], tag = kids.at(-2), body = kids.at(-1), tt = tyOf(strip(tag).type);
|
|
756
|
+
if (kids.length > 2)
|
|
757
|
+
return sno(n, 'a switch with a declaration');
|
|
758
|
+
const cases = [];
|
|
759
|
+
const label = (c) => {
|
|
760
|
+
if (c.kind === 'CaseStmt') {
|
|
761
|
+
if (c.inner.length > 2)
|
|
762
|
+
return void cases.push({ p: pos(c), b: sno(c, 'a case range (a GNU extension)') });
|
|
763
|
+
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: [] });
|
|
765
|
+
return c.inner[1];
|
|
766
|
+
}
|
|
767
|
+
if (c.kind === 'DefaultStmt') {
|
|
768
|
+
cases.push({ p: pos(c), default: true, b: [] });
|
|
769
|
+
return c.inner[0];
|
|
770
|
+
}
|
|
771
|
+
return c;
|
|
772
|
+
};
|
|
773
|
+
for (const c0 of body.kind === 'CompoundStmt' ? body.inner ?? [] : [body]) {
|
|
774
|
+
let c = c0;
|
|
775
|
+
while (c && (c.kind === 'CaseStmt' || c.kind === 'DefaultStmt')) {
|
|
776
|
+
const s = label(c);
|
|
777
|
+
c = s === c ? undefined : s;
|
|
778
|
+
}
|
|
779
|
+
if (c && cases.length)
|
|
780
|
+
cases.at(-1).b.push(...st(c));
|
|
781
|
+
}
|
|
782
|
+
// (labels with nothing between them are one case: case 1: case 2: ...; one with the default is the default)
|
|
783
|
+
for (let i = cases.length - 2; i >= 0; i--) {
|
|
784
|
+
const a = cases[i], b = cases[i + 1];
|
|
785
|
+
if (a.b.length)
|
|
786
|
+
continue;
|
|
787
|
+
if (a.default || b.default)
|
|
788
|
+
cases.splice(i, 2, { ...b, default: true, list: undefined, p: a.p });
|
|
789
|
+
else
|
|
790
|
+
cases.splice(i, 2, { ...b, list: [...a.list, ...b.list], p: a.p });
|
|
791
|
+
}
|
|
792
|
+
for (const c of cases)
|
|
793
|
+
if (c.default)
|
|
794
|
+
delete c.list;
|
|
795
|
+
cases.forEach((c, i) => { if (i < cases.length - 1 && !ends(c.b))
|
|
796
|
+
c.b.push({ s: 'branch', p: c.p, tok: 'fallthrough' }); });
|
|
797
|
+
return [{ s: 'switch', p: pos(n), tag: x(tag), cases }];
|
|
798
|
+
}
|
|
799
|
+
// --- the functions ----------------------------------------------------------------------------------------------------
|
|
800
|
+
const out = [], td0 = () => records.get(entryRec);
|
|
801
|
+
const fn = (name, f, pre = []) => {
|
|
802
|
+
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
|
+
for (const c of body.inner ?? [])
|
|
804
|
+
topStmts.add(c.id);
|
|
805
|
+
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 })),
|
|
808
|
+
results: T(rt).k === 'void' ? [] : [{ t: rt }], body: [...pre, ...ss(body.inner ?? [])],
|
|
809
|
+
...(/\.\.\.\)$/.test(f.type.qualType) ? { params: [{ t: -1, variadic: true }] } : {}),
|
|
810
|
+
static: f.storageClass === 'static', ...(f.virtual ? { why: `${name}: a virtual method (its dispatch)` } : {}) };
|
|
811
|
+
};
|
|
812
|
+
for (const [name, f] of funcs)
|
|
813
|
+
out.push(fn(name, f));
|
|
814
|
+
// The constructor (C++): its member initializers (each field's own in-class one where it has no other), then its body;
|
|
815
|
+
// the rule starts where it leaves the object, its parameters the rule's.
|
|
816
|
+
let ctor;
|
|
817
|
+
if (cpp) {
|
|
818
|
+
if (ctors.length > 1)
|
|
819
|
+
refuse(ctors[1], `${entry}: ${ctors.length} constructors (the C++ front end reads a class with one)`);
|
|
820
|
+
const c = ctors[0], inits = new Map();
|
|
821
|
+
for (const f of fieldNode.values())
|
|
822
|
+
if (fields.get(f.id).rec === entryRec && f.inner?.length && !f.inner[0].kind.endsWith('Attr'))
|
|
823
|
+
inits.set(f.id, f.inner[0]);
|
|
824
|
+
for (const k of c?.inner ?? [])
|
|
825
|
+
if (k.kind === 'CXXCtorInitializer') {
|
|
826
|
+
const d = k.anyInit;
|
|
827
|
+
if (d && k.inner?.length)
|
|
828
|
+
inits.set(d.id, fillDefault(k.inner[0], fieldNode.get(d.id)));
|
|
829
|
+
}
|
|
830
|
+
const pre = [];
|
|
831
|
+
for (const [id, v0] of inits) {
|
|
832
|
+
const f = fieldNode.get(id), ft = T(tyOf(f.type)), v = strip(v0);
|
|
833
|
+
if (ft.mutex || (v.kind === 'InitListExpr' && (v.inner ?? []).every(k => k.kind === 'ImplicitValueInitExpr')))
|
|
834
|
+
continue;
|
|
835
|
+
if (v.kind === 'CXXConstructExpr' && !v.inner?.length)
|
|
836
|
+
continue;
|
|
837
|
+
pre.push({ s: 'assign', p: pos(v0), op: '=', l: [{ e: 'sel', ...at(v0, tyOf(f.type)), k: 'field', x: { e: 'id', ...at(v0, add(`ptr:${entryT}`, { k: 'ptr', go: `${entry} *`, elem: entryT })), o: 'var', id: 'this', n: 'this' }, path: [fields.get(id).i] }], r: [x(v)] });
|
|
838
|
+
}
|
|
839
|
+
ctor = c ? fn('$ctor', Object.assign(c, { $m: true }), pre) : { name: '$ctor', p: pos(td0()), span: [0, 0], recv: { t: add(`ptr:${entryT}`, { k: 'ptr', go: `${entry} *`, elem: entryT }), ptr: true, type: entry, name: 'this', id: 'this' }, params: [], results: [], body: pre };
|
|
840
|
+
}
|
|
841
|
+
const td = td0();
|
|
842
|
+
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
|
+
// (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);
|
|
845
|
+
// (the class's qualified name, which the harness spells: `this`'s type, as clang prints it)
|
|
846
|
+
const thisOf = (n) => (n.kind === 'CXXThisExpr' ? n.type.qualType : (n.inner ?? []).map(thisOf).find(Boolean));
|
|
847
|
+
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 };
|
|
849
|
+
}
|
|
850
|
+
// Where a function type's result ends: before its parameter list (the last parenthesized group).
|
|
851
|
+
function retEnd(s) {
|
|
852
|
+
let d = 0;
|
|
853
|
+
for (let i = s.length - 1; i >= 0; i--) {
|
|
854
|
+
if (s[i] === ')')
|
|
855
|
+
d++;
|
|
856
|
+
else if (s[i] === '(' && --d === 0)
|
|
857
|
+
return i;
|
|
858
|
+
}
|
|
859
|
+
return s.length;
|
|
860
|
+
}
|