Skip to content

Commit 7b5e680

Browse files
committed
Merge origin/main into fix/py-namespace-reexports-v2 (pick up DeusData#2680, DeusData#2690, DeusData#2695, DeusData#2696)
py_lsp.c: the module-member lookup of this branch and the C3 super() lookup of DeusData#2696 were added at the same place; both are kept. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
2 parents 0ae936f + c06465b commit 7b5e680

14 files changed

Lines changed: 1250 additions & 95 deletions

‎internal/cbm/extract_defs.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11493,9 +11493,12 @@ static void push_nested_class_nodes(TSNode body, const CBMLangSpec *spec, wd_sta
1149311493
wd_push(s, child, enclosing_qn);
1149411494
} else {
1149511495
const char *ck = ts_node_type(child);
11496-
/* A nested class inside #if/#else is still nested here. */
11496+
/* A nested class inside #if/#else is still nested here, and so is
11497+
* a decorated one (Python `@dataclass class Meta:` in a class
11498+
* body): the decorated_definition wraps the class_definition. */
1149711499
if (strcmp(ck, "field_declaration") == 0 ||
1149811500
strcmp(ck, "template_declaration") == 0 || strcmp(ck, "declaration") == 0 ||
11501+
(ctx->language == CBM_LANG_PYTHON && strcmp(ck, "decorated_definition") == 0) ||
1149911502
cbm_kind_in_set(child, cbm_conditional_block_types(spec->language))) {
1150011503
ts_nstack_push(&nc_stack, child);
1150111504
}

‎internal/cbm/extract_imports.c‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,19 @@ static const char *python_import_root(CBMArena *a, const char *path) {
123123
return dot ? cbm_arena_strndup(a, path, (size_t)(dot - path)) : path;
124124
}
125125

126+
/* Spell `from <module> import <name>` as one dotted path. A package-relative
127+
* module (`from . import x`, `from .. import x`) is nothing but dots, which
128+
* already end in the separator: joining with another '.' would add a level and
129+
* name the PARENT package ("..x"), so the import resolved one package too high
130+
* and fell to a same-named definition anywhere in the project. */
131+
static const char *python_join_from_import(CBMArena *a, const char *module, const char *name) {
132+
size_t len = strlen(module);
133+
if (len > 0 && module[len - SKIP_ONE] == '.') {
134+
return cbm_arena_sprintf(a, "%s%s", module, name);
135+
}
136+
return cbm_arena_sprintf(a, "%s.%s", module, name);
137+
}
138+
126139
// --- Go imports ---
127140
// import_declaration -> import_spec_list -> import_spec -> (name, path)
128141

@@ -209,7 +222,7 @@ static void emit_py_aliased_import(CBMExtractCtx *ctx, TSNode child, const char
209222
}
210223
const char *local = !ts_node_is_null(alias_node) ? cbm_node_text(a, alias_node, ctx->source)
211224
: path_last(a, name);
212-
const char *full = mod_prefix ? cbm_arena_sprintf(a, "%s.%s", mod_prefix, name) : name;
225+
const char *full = mod_prefix ? python_join_from_import(a, mod_prefix, name) : name;
213226
CBMImport imp = {.local_name = local, .module_path = full};
214227
cbm_imports_push(&ctx->result->imports, a, imp);
215228
}
@@ -267,7 +280,7 @@ static void emit_py_import_from_name(CBMExtractCtx *ctx, TSNode child, const cha
267280
CBMArena *a = ctx->arena;
268281
char *name = cbm_node_text(a, child, ctx->source);
269282
if (name && name[0]) {
270-
const char *full = mod_path ? cbm_arena_sprintf(a, "%s.%s", mod_path, name) : name;
283+
const char *full = mod_path ? python_join_from_import(a, mod_path, name) : name;
271284
CBMImport imp = {.local_name = name, .module_path = full};
272285
cbm_imports_push(&ctx->result->imports, a, imp);
273286
}

‎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
@@ -1352,6 +1352,138 @@ static void py_emit_module_member_call(PyLSPContext *ctx, const char *callee_qn,
13521352
}
13531353
}
13541354

1355+
/* ── super(): the C3 method resolution order ──────────────────────── */
1356+
1357+
/* Longest linearization followed. A longer one (or more bases than this) keeps
1358+
* the base-by-base walk of the caller. */
1359+
#define PY_C3_MAX 64
1360+
1361+
/* The linearizations computed during one super() lookup: every class of a
1362+
* diamond is linearized once, however many paths reach it. */
1363+
typedef struct {
1364+
const char *qn[PY_C3_MAX];
1365+
const char **mro[PY_C3_MAX];
1366+
int len[PY_C3_MAX];
1367+
int count;
1368+
} py_c3_cache_t;
1369+
1370+
static bool py_c3_in_tail(const char *qn, const char *const *seq, int head, int len) {
1371+
for (int k = head + 1; k < len; k++) {
1372+
if (strcmp(seq[k], qn) == 0)
1373+
return true;
1374+
}
1375+
return false;
1376+
}
1377+
1378+
/* The C3 head to take next: the first sequence head that is in no sequence's
1379+
* tail. NULL when every head is blocked (Python rejects such a class). */
1380+
static const char *py_c3_pick(const char **const *seqs, const int *lens, const int *heads,
1381+
int nseq) {
1382+
for (int s = 0; s < nseq; s++) {
1383+
if (heads[s] >= lens[s])
1384+
continue;
1385+
const char *cand = seqs[s][heads[s]];
1386+
bool blocked = false;
1387+
for (int t = 0; t < nseq && !blocked; t++) {
1388+
blocked = heads[t] < lens[t] && py_c3_in_tail(cand, seqs[t], heads[t], lens[t]);
1389+
}
1390+
if (!blocked)
1391+
return cand;
1392+
}
1393+
return NULL;
1394+
}
1395+
1396+
/* Python's method resolution order of `type_qn`: the class, then the C3 merge
1397+
* of its bases' linearizations and of the base list. A common base therefore
1398+
* follows every class deriving from it. Sets *out to an arena array and returns
1399+
* its length, or -1 when there is no C3 order, the hierarchy is deeper than
1400+
* CBM_LSP_MAX_LOOKUP_DEPTH, or the order is longer than PY_C3_MAX. A base the
1401+
* registry does not know (external, unresolved) linearizes to itself. */
1402+
static int py_c3_linearize(PyLSPContext *ctx, py_c3_cache_t *cache, const char *type_qn,
1403+
const char ***out, int depth) {
1404+
for (int c = 0; c < cache->count; c++) {
1405+
if (strcmp(cache->qn[c], type_qn) == 0) {
1406+
*out = cache->mro[c];
1407+
return cache->len[c];
1408+
}
1409+
}
1410+
if (depth > CBM_LSP_MAX_LOOKUP_DEPTH)
1411+
return -1;
1412+
const CBMRegisteredType *rt = cbm_registry_lookup_type(ctx->registry, type_qn);
1413+
if (rt && rt->alias_of)
1414+
return py_c3_linearize(ctx, cache, rt->alias_of, out, depth + 1);
1415+
const char **bases = rt ? rt->embedded_types : NULL;
1416+
int nb = 0;
1417+
while (bases && bases[nb])
1418+
nb++;
1419+
if (nb >= PY_C3_MAX)
1420+
return -1;
1421+
const char **seqs[PY_C3_MAX];
1422+
int lens[PY_C3_MAX];
1423+
int heads[PY_C3_MAX];
1424+
for (int b = 0; b < nb; b++) {
1425+
lens[b] = py_c3_linearize(ctx, cache, bases[b], &seqs[b], depth + 1);
1426+
if (lens[b] < 0)
1427+
return -1;
1428+
heads[b] = 0;
1429+
}
1430+
seqs[nb] = bases;
1431+
lens[nb] = nb;
1432+
heads[nb] = 0;
1433+
const char **mro = (const char **)cbm_arena_alloc(ctx->arena, PY_C3_MAX * sizeof(char *));
1434+
if (!mro)
1435+
return -1;
1436+
int n = 0;
1437+
mro[n++] = type_qn;
1438+
for (;;) {
1439+
bool pending = false;
1440+
for (int s = 0; s <= nb && !pending; s++) {
1441+
pending = heads[s] < lens[s];
1442+
}
1443+
if (!pending)
1444+
break;
1445+
const char *pick = py_c3_pick((const char **const *)seqs, lens, heads, nb + 1);
1446+
if (!pick || n >= PY_C3_MAX)
1447+
return -1;
1448+
mro[n++] = pick;
1449+
for (int s = 0; s <= nb; s++) {
1450+
if (heads[s] < lens[s] && strcmp(seqs[s][heads[s]], pick) == 0)
1451+
heads[s]++;
1452+
}
1453+
}
1454+
if (cache->count < PY_C3_MAX) {
1455+
cache->qn[cache->count] = type_qn;
1456+
cache->mro[cache->count] = mro;
1457+
cache->len[cache->count] = n;
1458+
cache->count++;
1459+
}
1460+
*out = mro;
1461+
return n;
1462+
}
1463+
1464+
/* The method super() reaches from inside `class_qn`: the first class AFTER it in
1465+
* its C3 order that declares `member` itself. *have_mro is false when the
1466+
* hierarchy has no C3 order; the caller then keeps its base-by-base walk. */
1467+
static const CBMRegisteredFunc *py_lookup_super_attribute(PyLSPContext *ctx, const char *class_qn,
1468+
const char *member, bool *have_mro) {
1469+
*have_mro = false;
1470+
if (!ctx || !class_qn || !member)
1471+
return NULL;
1472+
py_c3_cache_t cache;
1473+
cache.count = 0;
1474+
const char **mro = NULL;
1475+
int n = py_c3_linearize(ctx, &cache, class_qn, &mro, 0);
1476+
if (n < 0)
1477+
return NULL;
1478+
*have_mro = true;
1479+
for (int i = 1; i < n; i++) {
1480+
const CBMRegisteredFunc *f = cbm_registry_lookup_method(ctx->registry, mro[i], member);
1481+
if (f)
1482+
return f;
1483+
}
1484+
return NULL;
1485+
}
1486+
13551487
/* Per-file field overlay: look up a (class_qn, field_name) recorded during resolve
13561488
* against the sealed shared registry. Linear scan — the overlay only holds fields
13571489
* discovered in THIS file's own classes, so it is small. */
@@ -3089,6 +3221,22 @@ static void py_emit_call_for(PyLSPContext *ctx, TSNode call_node) {
30893221
const CBMRegisteredType *enclosing =
30903222
cbm_registry_lookup_type(ctx->registry, ctx->enclosing_class_qn);
30913223
if (enclosing && enclosing->embedded_types) {
3224+
// The proxy walks the enclosing class's C3 order, in which a
3225+
// diamond's shared base follows every class deriving from it:
3226+
// RadarrCalendarEntity(RadarrEntity, CalendarEntity) reaches
3227+
// CalendarEntity before Entity, which the first base reaches
3228+
// through its own ancestry. The base-by-base walk below stays for
3229+
// a hierarchy without a C3 order and for an undeclared __init__.
3230+
bool have_mro = false;
3231+
const CBMRegisteredFunc *sf = py_lookup_super_attribute(
3232+
ctx, ctx->enclosing_class_qn, attr_name, &have_mro);
3233+
if (sf) {
3234+
bool is_init = strcmp(attr_name, "__init__") == 0;
3235+
py_emit_resolved_call(ctx, sf->qualified_name,
3236+
is_init ? "lsp_super_init" : "lsp_super",
3237+
is_init ? 0.90f : 0.88f, call_node);
3238+
return;
3239+
}
30923240
for (int i = 0; enclosing->embedded_types[i]; i++) {
30933241
// super().__init__() is a constructor delegation:
30943242
// 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_lsp_cross.c‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,16 @@ static const char *pxc_python_dotted_base_qn(const pxc_base_scope_t *bs, const c
305305
* Returns NULL when the base is not a confidently-known project type;
306306
* stdlib and third-party bases land here and keep their raw spelling. */
307307
static const char *pxc_resolve_base_qn(const pxc_base_scope_t *bs, const char *raw,
308-
const char *module_qn) {
308+
const char *module_qn, const char *class_qn) {
309309
if (!bs || !bs->reg || !raw || !raw[0]) {
310310
return NULL;
311311
}
312+
/* A base of a class nested in a class body: an earlier sibling first. */
313+
const char *sibling =
314+
cbm_registry_resolve_python_class_body_base(bs->reg, bs->gbuf, class_qn, raw, bs->lang);
315+
if (sibling) {
316+
return sibling;
317+
}
312318
const char *dotted = pxc_python_dotted_base_qn(bs, raw);
313319
if (dotted) {
314320
return dotted;
@@ -338,7 +344,8 @@ static const char *pxc_resolve_base_qn(const pxc_base_scope_t *bs, const char *r
338344
* source spelling. Unresolved entries pass through verbatim so a base the
339345
* registry does not know keeps working exactly as before. */
340346
static const char *pxc_join_base_qns(CBMArena *arena, const char *const *bases,
341-
const pxc_base_scope_t *bs, const char *module_qn) {
347+
const pxc_base_scope_t *bs, const char *module_qn,
348+
const char *class_qn) {
342349
if (!bases || !bases[0]) {
343350
return NULL;
344351
}
@@ -352,7 +359,7 @@ static const char *pxc_join_base_qns(CBMArena *arena, const char *const *bases,
352359
return pxc_join_pipe(arena, bases);
353360
}
354361
for (int i = 0; i < count; i++) {
355-
const char *qn = pxc_resolve_base_qn(bs, bases[i], module_qn);
362+
const char *qn = pxc_resolve_base_qn(bs, bases[i], module_qn, class_qn);
356363
resolved[i] = qn ? qn : bases[i];
357364
}
358365
resolved[count] = NULL;
@@ -520,9 +527,10 @@ static int pxc_build_lsp_def(CBMArena *arena, const CBMDefinition *src, const ch
520527
/* Languages whose cross registrars read embedded_types as QNs get their
521528
* bases resolved against the project registry; everyone else keeps the
522529
* raw source spelling their own registrar already knows how to handle. */
523-
dst->embedded_types = (bs && bs->reg && pxc_lang_resolves_base_qns(lang))
524-
? pxc_join_base_qns(arena, src->base_classes, bs, module_qn)
525-
: pxc_join_pipe(arena, src->base_classes);
530+
dst->embedded_types =
531+
(bs && bs->reg && pxc_lang_resolves_base_qns(lang))
532+
? pxc_join_base_qns(arena, src->base_classes, bs, module_qn, src->qualified_name)
533+
: pxc_join_pipe(arena, src->base_classes);
526534
dst->signature_param_types = NULL;
527535
dst->signature_param_count = 0;
528536
if (src->signature_param_types && src->signature_param_count > 0) {
@@ -727,7 +735,7 @@ static void pxc_fold_py_field_types(CBMArena *arena, const CBMFileResult *result
727735
fields[f].type_qn = NULL;
728736
CBMLSPDef *owner = ft->class_qn ? (CBMLSPDef *)cbm_ht_get(by_qn, ft->class_qn) : NULL;
729737
const char *name = owner ? pxc_py_annotation_type_name(arena, ft->type_text) : NULL;
730-
const char *qn = name ? pxc_resolve_base_qn(bs, name, module_qn) : NULL;
738+
const char *qn = name ? pxc_resolve_base_qn(bs, name, module_qn, NULL) : NULL;
731739
if (!qn || !ft->field_name || !ft->field_name[0]) {
732740
continue;
733741
}

‎src/pipeline/pass_parallel.c‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3910,13 +3910,17 @@ static void resolve_def_inherits(resolve_ctx_t *rc, resolve_worker_state_t *ws,
39103910
if (cbm_pipeline_php_vendor_bound(php_vendor, def->base_classes[b])) {
39113911
continue;
39123912
}
3913-
const char *bqn =
3914-
resolve_as_class(rc->registry, def->base_classes[b], mq, ik, iv, ic, fs->lang);
3915-
/* Same external-base gate as the sequential semantic pass. */
3916-
if (!bqn ||
3917-
cbm_python_external_base_contradicts(fs->lang, fs->imports, def->base_classes[b], bqn,
3918-
rc->main_gbuf, rc->project_name, fs->rel)) {
3919-
continue;
3913+
/* Same order as the sequential semantic pass: an earlier sibling of
3914+
* the class body first (no external-base gate), then the registry. */
3915+
const char *bqn = cbm_registry_resolve_python_class_body_base(
3916+
rc->registry, rc->main_gbuf, def->qualified_name, def->base_classes[b], fs->lang);
3917+
if (!bqn) {
3918+
bqn = resolve_as_class(rc->registry, def->base_classes[b], mq, ik, iv, ic, fs->lang);
3919+
if (!bqn || cbm_python_external_base_contradicts(
3920+
fs->lang, fs->imports, def->base_classes[b], bqn, rc->main_gbuf,
3921+
rc->project_name, fs->rel)) {
3922+
continue;
3923+
}
39203924
}
39213925
const cbm_gbuf_node_t *bn = cbm_gbuf_find_by_qn(rc->main_gbuf, bqn);
39223926
if (bn && node->id != bn->id) {

0 commit comments

Comments
 (0)