Skip to content

Commit c5b6f22

Browse files
committed
Merge origin/main into fix/py-nested-class-qn-v3 (DeusData#2690 landed as 4a057e4)
Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
2 parents 7578816 + 4a057e4 commit c5b6f22

10 files changed

Lines changed: 805 additions & 102 deletions

File tree

‎internal/cbm/extract_unified.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,20 @@ static const char *compute_class_qn(CBMExtractCtx *ctx, TSNode node, const WalkS
11761176
if (!name || !name[0]) {
11771177
return NULL;
11781178
}
1179+
/* Rust: the impl's type text carries its generic arguments (`impl<T> Gen<T>`,
1180+
* `impl<'a> Parser<'a>`), which the def side strips before naming the type
1181+
* (extract_defs.c extract_rust_impl: `Buffer<T>` -> `Buffer`). Strip them the
1182+
* same way, or every method of a generic impl gets a scope QN no node carries
1183+
* and all its calls fall back to the File node. */
1184+
if (ctx->language == CBM_LANG_RUST && strcmp(ts_node_type(node), "impl_item") == 0) {
1185+
char *lt = strchr(name, '<');
1186+
if (lt == name) {
1187+
return NULL; /* `<T as Tr>`-style self type: no name to scope by */
1188+
}
1189+
if (lt) {
1190+
*lt = '\0';
1191+
}
1192+
}
11791193

11801194
/* Nested class: prefix with the enclosing class QN (Outer.Inner) so this
11811195
* scope QN matches the def-side class QN (extract_defs.c compute_class_qn /

‎internal/cbm/lsp/py_lsp.c‎

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,138 @@ static const CBMRegisteredFunc *py_lookup_attribute(PyLSPContext *ctx, const cha
12291229
return py_lookup_attribute_depth(ctx, type_qn, member_name, 0);
12301230
}
12311231

1232+
/* ── super(): the C3 method resolution order ──────────────────────── */
1233+
1234+
/* Longest linearization followed. A longer one (or more bases than this) keeps
1235+
* the base-by-base walk of the caller. */
1236+
#define PY_C3_MAX 64
1237+
1238+
/* The linearizations computed during one super() lookup: every class of a
1239+
* diamond is linearized once, however many paths reach it. */
1240+
typedef struct {
1241+
const char *qn[PY_C3_MAX];
1242+
const char **mro[PY_C3_MAX];
1243+
int len[PY_C3_MAX];
1244+
int count;
1245+
} py_c3_cache_t;
1246+
1247+
static bool py_c3_in_tail(const char *qn, const char *const *seq, int head, int len) {
1248+
for (int k = head + 1; k < len; k++) {
1249+
if (strcmp(seq[k], qn) == 0)
1250+
return true;
1251+
}
1252+
return false;
1253+
}
1254+
1255+
/* The C3 head to take next: the first sequence head that is in no sequence's
1256+
* tail. NULL when every head is blocked (Python rejects such a class). */
1257+
static const char *py_c3_pick(const char **const *seqs, const int *lens, const int *heads,
1258+
int nseq) {
1259+
for (int s = 0; s < nseq; s++) {
1260+
if (heads[s] >= lens[s])
1261+
continue;
1262+
const char *cand = seqs[s][heads[s]];
1263+
bool blocked = false;
1264+
for (int t = 0; t < nseq && !blocked; t++) {
1265+
blocked = heads[t] < lens[t] && py_c3_in_tail(cand, seqs[t], heads[t], lens[t]);
1266+
}
1267+
if (!blocked)
1268+
return cand;
1269+
}
1270+
return NULL;
1271+
}
1272+
1273+
/* Python's method resolution order of `type_qn`: the class, then the C3 merge
1274+
* of its bases' linearizations and of the base list. A common base therefore
1275+
* follows every class deriving from it. Sets *out to an arena array and returns
1276+
* its length, or -1 when there is no C3 order, the hierarchy is deeper than
1277+
* CBM_LSP_MAX_LOOKUP_DEPTH, or the order is longer than PY_C3_MAX. A base the
1278+
* registry does not know (external, unresolved) linearizes to itself. */
1279+
static int py_c3_linearize(PyLSPContext *ctx, py_c3_cache_t *cache, const char *type_qn,
1280+
const char ***out, int depth) {
1281+
for (int c = 0; c < cache->count; c++) {
1282+
if (strcmp(cache->qn[c], type_qn) == 0) {
1283+
*out = cache->mro[c];
1284+
return cache->len[c];
1285+
}
1286+
}
1287+
if (depth > CBM_LSP_MAX_LOOKUP_DEPTH)
1288+
return -1;
1289+
const CBMRegisteredType *rt = cbm_registry_lookup_type(ctx->registry, type_qn);
1290+
if (rt && rt->alias_of)
1291+
return py_c3_linearize(ctx, cache, rt->alias_of, out, depth + 1);
1292+
const char **bases = rt ? rt->embedded_types : NULL;
1293+
int nb = 0;
1294+
while (bases && bases[nb])
1295+
nb++;
1296+
if (nb >= PY_C3_MAX)
1297+
return -1;
1298+
const char **seqs[PY_C3_MAX];
1299+
int lens[PY_C3_MAX];
1300+
int heads[PY_C3_MAX];
1301+
for (int b = 0; b < nb; b++) {
1302+
lens[b] = py_c3_linearize(ctx, cache, bases[b], &seqs[b], depth + 1);
1303+
if (lens[b] < 0)
1304+
return -1;
1305+
heads[b] = 0;
1306+
}
1307+
seqs[nb] = bases;
1308+
lens[nb] = nb;
1309+
heads[nb] = 0;
1310+
const char **mro = (const char **)cbm_arena_alloc(ctx->arena, PY_C3_MAX * sizeof(char *));
1311+
if (!mro)
1312+
return -1;
1313+
int n = 0;
1314+
mro[n++] = type_qn;
1315+
for (;;) {
1316+
bool pending = false;
1317+
for (int s = 0; s <= nb && !pending; s++) {
1318+
pending = heads[s] < lens[s];
1319+
}
1320+
if (!pending)
1321+
break;
1322+
const char *pick = py_c3_pick((const char **const *)seqs, lens, heads, nb + 1);
1323+
if (!pick || n >= PY_C3_MAX)
1324+
return -1;
1325+
mro[n++] = pick;
1326+
for (int s = 0; s <= nb; s++) {
1327+
if (heads[s] < lens[s] && strcmp(seqs[s][heads[s]], pick) == 0)
1328+
heads[s]++;
1329+
}
1330+
}
1331+
if (cache->count < PY_C3_MAX) {
1332+
cache->qn[cache->count] = type_qn;
1333+
cache->mro[cache->count] = mro;
1334+
cache->len[cache->count] = n;
1335+
cache->count++;
1336+
}
1337+
*out = mro;
1338+
return n;
1339+
}
1340+
1341+
/* The method super() reaches from inside `class_qn`: the first class AFTER it in
1342+
* its C3 order that declares `member` itself. *have_mro is false when the
1343+
* hierarchy has no C3 order; the caller then keeps its base-by-base walk. */
1344+
static const CBMRegisteredFunc *py_lookup_super_attribute(PyLSPContext *ctx, const char *class_qn,
1345+
const char *member, bool *have_mro) {
1346+
*have_mro = false;
1347+
if (!ctx || !class_qn || !member)
1348+
return NULL;
1349+
py_c3_cache_t cache;
1350+
cache.count = 0;
1351+
const char **mro = NULL;
1352+
int n = py_c3_linearize(ctx, &cache, class_qn, &mro, 0);
1353+
if (n < 0)
1354+
return NULL;
1355+
*have_mro = true;
1356+
for (int i = 1; i < n; i++) {
1357+
const CBMRegisteredFunc *f = cbm_registry_lookup_method(ctx->registry, mro[i], member);
1358+
if (f)
1359+
return f;
1360+
}
1361+
return NULL;
1362+
}
1363+
12321364
/* Per-file field overlay: look up a (class_qn, field_name) recorded during resolve
12331365
* against the sealed shared registry. Linear scan — the overlay only holds fields
12341366
* discovered in THIS file's own classes, so it is small. */
@@ -2964,6 +3096,22 @@ static void py_emit_call_for(PyLSPContext *ctx, TSNode call_node) {
29643096
const CBMRegisteredType *enclosing =
29653097
cbm_registry_lookup_type(ctx->registry, ctx->enclosing_class_qn);
29663098
if (enclosing && enclosing->embedded_types) {
3099+
// The proxy walks the enclosing class's C3 order, in which a
3100+
// diamond's shared base follows every class deriving from it:
3101+
// RadarrCalendarEntity(RadarrEntity, CalendarEntity) reaches
3102+
// CalendarEntity before Entity, which the first base reaches
3103+
// through its own ancestry. The base-by-base walk below stays for
3104+
// a hierarchy without a C3 order and for an undeclared __init__.
3105+
bool have_mro = false;
3106+
const CBMRegisteredFunc *sf = py_lookup_super_attribute(
3107+
ctx, ctx->enclosing_class_qn, attr_name, &have_mro);
3108+
if (sf) {
3109+
bool is_init = strcmp(attr_name, "__init__") == 0;
3110+
py_emit_resolved_call(ctx, sf->qualified_name,
3111+
is_init ? "lsp_super_init" : "lsp_super",
3112+
is_init ? 0.90f : 0.88f, call_node);
3113+
return;
3114+
}
29673115
for (int i = 0; enclosing->embedded_types[i]; i++) {
29683116
// super().__init__() is a constructor delegation:
29693117
// lsp_super_init is the MORE SPECIFIC, more accurate

‎internal/cbm/lsp/rust_lsp.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,22 @@ static void rust_strip_turbofish(char *path) {
306306
*write = '\0';
307307
}
308308

309+
/* In place: `Gen<T>` -> `Gen`, `foo::Bar<'a, T>` -> `foo::Bar` (#763). An
310+
* impl's self type keeps its generic arguments in the source text, but every
311+
* def-side QN (a method's receiver, the extractor's call scope) strips them.
312+
* Unstripped, the LSP's caller QN, `self`/`Self` dispatch and the return-type
313+
* harvest never matched a generic impl's methods. Text starting with `<` (a
314+
* qualified path) is left alone. Safe on NULL. */
315+
static void rust_strip_impl_type_args(char *type_text) {
316+
if (!type_text) {
317+
return;
318+
}
319+
char *lt = strchr(type_text, '<');
320+
if (lt && lt > type_text) {
321+
*lt = '\0';
322+
}
323+
}
324+
309325
/* Look up a `use` alias and return its fully-qualified module path,
310326
* or NULL if absent. The returned pointer aliases into the use map. */
311327
static const char *rust_resolve_use(RustLSPContext *ctx, const char *local_name) {
@@ -5274,6 +5290,7 @@ static void rust_process_impl(RustLSPContext *ctx, TSNode impl_node) {
52745290
char *type_text = rust_node_text(ctx, type_node);
52755291
if (!type_text)
52765292
return;
5293+
rust_strip_impl_type_args(type_text);
52775294

52785295
/* Detect blanket impl: `impl<T: Trait> ForeignTrait for T { ... }`
52795296
* where type_text is a name that appears in the impl's type
@@ -6028,6 +6045,7 @@ void cbm_rust_build_local_registry(CBMArena *arena, CBMTypeRegistry *reg, CBMFil
60286045
if (ts_node_is_null(type_node) || ts_node_is_null(body))
60296046
continue;
60306047
char *type_name = cbm_node_text(arena, type_node, source);
6048+
rust_strip_impl_type_args(type_name);
60316049
if (!type_name || !type_name[0])
60326050
continue;
60336051
const char *type_qn = cbm_arena_sprintf(arena, "%s.%s", module_qn, type_name);

‎src/pipeline/pass_definitions.c‎

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ static void build_def_props(char *buf, size_t bufsize, const CBMDefinition *def)
309309
}
310310
size_t pos = (size_t)n;
311311
append_json_string(buf, bufsize, &pos, "docstring", def->docstring);
312-
/* Right after the docstring: the buffer is sized for exactly these three
313-
* uncapped fields (pd_props_buf), so none can be squeezed out. */
312+
/* The buffer is sized for every field below (pd_props_buf), so none can be
313+
* squeezed out. */
314314
append_json_raw(buf, bufsize, &pos, "variants", def->variants);
315315
append_json_string(buf, bufsize, &pos, "variant_key", def->variant_key);
316316
append_json_string(buf, bufsize, &pos, "signature", def->signature);
@@ -353,24 +353,71 @@ static void build_def_props(char *buf, size_t bufsize, const CBMDefinition *def)
353353
}
354354
}
355355

356-
/* A def's properties buffer: CBM_SZ_2K for every other field plus the whole
357-
* serialized docstring, variants and variant_key fields, which have no length
358-
* cap (a field that does not fit is dropped whole). Returns `stack` for a def
359-
* with none of them, or when the larger buffer cannot be allocated. Twin of
360-
* pass_parallel.c -- keep both in sync. */
361-
static char *pd_props_buf(const CBMDefinition *def, char *stack, size_t *size) {
362-
size_t need = *size;
363-
if (def->docstring && def->docstring[0]) {
364-
need += strlen("docstring") + def_json_escaped_len(def->docstring) + PD_JSON_FIELD_OVERHEAD;
356+
/* Upper bound of build_def_props' fixed prefix: the Function/Method metrics
357+
* literal (about 330 B) with nine ints and seven true/false/test_role strings at
358+
* their widest stays under 600 B. */
359+
enum { PD_PROPS_PREFIX_MAX = CBM_SZ_1K };
360+
361+
/* Serialized size of one optional field as append_json_string emits it
362+
* (0 when the field is absent). */
363+
static size_t pd_str_field_len(const char *key, const char *val) {
364+
return val && val[0] ? strlen(key) + def_json_escaped_len(val) + PD_JSON_FIELD_OVERHEAD : 0;
365+
}
366+
367+
/* Serialized size of a string array as append_json_str_array emits it. */
368+
static size_t pd_arr_field_len(const char *key, const char **arr) {
369+
if (!arr || !arr[0]) {
370+
return 0;
371+
}
372+
size_t n = strlen(key) + PD_JSON_FIELD_OVERHEAD;
373+
for (int i = 0; arr[i]; i++) {
374+
n += def_json_escaped_len(arr[i]) + PD_ESC_SPACE + (i > 0 ? SKIP_ONE : 0);
365375
}
376+
return n;
377+
}
378+
379+
/* A def's properties buffer, sized so EVERY field build_def_props appends fits:
380+
* the fixed metrics prefix (PD_PROPS_PREFIX_MAX) plus each optional field's
381+
* whole serialized form. A field that does not fit is dropped whole, so a buffer
382+
* sized for only some of them let a long signature, decorator list or parameter
383+
* list squeeze out the MinHash fingerprint ("fp"), structural profile ("sp") and
384+
* body tokens ("bt") behind them: no SIMILAR_TO edge and fewer
385+
* SEMANTICALLY_RELATED edges for exactly the widest definitions (jellyfin's
386+
* ImageController actions, 2026-10-18). fp/sp/bt are appended behind their own
387+
* room checks, so both the check's reserve and the field are counted. Returns
388+
* `stack` when everything fits it, or when the larger buffer cannot be
389+
* allocated. Twin of pass_parallel.c -- keep both in sync. */
390+
static char *pd_props_buf(const CBMDefinition *def, char *stack, size_t *size) {
391+
size_t need = PD_PROPS_PREFIX_MAX + pd_str_field_len("docstring", def->docstring);
366392
if (def->variants && def->variants[0]) {
367393
need += strlen("variants") + strlen(def->variants) + PD_JSON_FIELD_OVERHEAD;
368394
}
369-
if (def->variant_key && def->variant_key[0]) {
370-
need +=
371-
strlen("variant_key") + def_json_escaped_len(def->variant_key) + PD_JSON_FIELD_OVERHEAD;
395+
need += pd_str_field_len("variant_key", def->variant_key) +
396+
pd_str_field_len("signature", def->signature) +
397+
pd_str_field_len("return_type", def->return_type) +
398+
pd_str_field_len("parent_class", def->parent_class) +
399+
pd_arr_field_len("decorators", def->decorators) +
400+
pd_arr_field_len("base_classes", def->base_classes) +
401+
pd_arr_field_len("param_names", def->param_names) +
402+
pd_arr_field_len("param_types", def->param_types) +
403+
pd_str_field_len("route_path", def->route_path) +
404+
pd_str_field_len("route_method", def->route_method) +
405+
pd_str_field_len("http_client", def->http_client) +
406+
pd_str_field_len("http_base_url", def->http_base_url);
407+
for (const char **kv = def->extra_props; kv && kv[0] && kv[SKIP_ONE]; kv += PAIR_LEN) {
408+
need += pd_str_field_len(kv[0], kv[SKIP_ONE]);
409+
}
410+
if (def->fingerprint && def->fingerprint_k > 0) {
411+
need += CBM_MINHASH_HEX_LEN + CBM_MINHASH_JSON_OVERHEAD + SKIP_ONE;
412+
}
413+
if (def->structural_profile) {
414+
need += CBM_AST_PROFILE_BUF + pd_str_field_len("sp", def->structural_profile) + SKIP_ONE;
415+
}
416+
if (def->body_tokens) {
417+
need += CBM_SZ_512 + pd_str_field_len("bt", def->body_tokens) + SKIP_ONE;
372418
}
373-
if (need == *size) {
419+
need += PD_ESC_SPACE; /* the closing '}' + NUL */
420+
if (need <= *size) {
374421
return stack;
375422
}
376423
char *buf = cbm_alloc(CBM_MEM_CLASS_GBUF_STRING, need);

0 commit comments

Comments
 (0)