Skip to content

Commit 2cee7a1

Browse files
committed
Merge remote-tracking branch 'origin/main' into fix/issue-1692-aspnet-routes-v2
Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
2 parents 16b2e94 + 4527fc7 commit 2cee7a1

16 files changed

Lines changed: 1196 additions & 23 deletions

‎internal/cbm/extract_defs.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11816,9 +11816,12 @@ static void push_nested_class_nodes(TSNode body, const CBMLangSpec *spec, wd_sta
1181611816
wd_push(s, child, enclosing_qn);
1181711817
} else {
1181811818
const char *ck = ts_node_type(child);
11819-
/* A nested class inside #if/#else is still nested here. */
11819+
/* A nested class inside #if/#else is still nested here, and so is
11820+
* a decorated one (Python `@dataclass class Meta:` in a class
11821+
* body): the decorated_definition wraps the class_definition. */
1182011822
if (strcmp(ck, "field_declaration") == 0 ||
1182111823
strcmp(ck, "template_declaration") == 0 || strcmp(ck, "declaration") == 0 ||
11824+
(ctx->language == CBM_LANG_PYTHON && strcmp(ck, "decorated_definition") == 0) ||
1182211825
cbm_kind_in_set(child, cbm_conditional_block_types(spec->language))) {
1182311826
ts_nstack_push(&nc_stack, child);
1182411827
}

‎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/lsp_work.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@ extern _Thread_local uint64_t cbm_lsp_work_steps;
1717
#define CBM_LSP_WORK(n) (cbm_lsp_work_steps += (uint64_t)(n))
1818
/* Read and reset the calling thread's counter. */
1919
uint64_t cbm_lsp_work_take(void);
20+
/* Test-only work ceiling (0 = none). A regression test that reverts a bound
21+
* sets it so the reverted, unbounded loop stops deterministically and the test
22+
* reports RED instead of hanging the runner. Seam builds only: a product build
23+
* has no ceiling, so it can never decide graph content. */
24+
extern _Thread_local uint64_t cbm_lsp_work_ceiling;
25+
#define CBM_LSP_WORK_CEILING_HIT() \
26+
(cbm_lsp_work_ceiling != 0 && cbm_lsp_work_steps > cbm_lsp_work_ceiling)
2027
#else
2128
#define CBM_LSP_WORK(n) ((void)0)
29+
#define CBM_LSP_WORK_CEILING_HIT() (0)
2230
#endif
2331

2432
#endif /* CBM_LSP_WORK_H */

‎internal/cbm/lsp/php_lsp.c‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4372,6 +4372,17 @@ void cbm_run_php_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
43724372
}
43734373
}
43744374

4375+
/* Index the registry (F12). Unindexed, every lookup below was a linear
4376+
* scan over the stdlib plus every definition of the file: O(lookups x
4377+
* definitions), quadratic in the size of one file. First-registered
4378+
* answers keep the result of that scan (the first registration of a
4379+
* duplicate QN wins), so the output does not change. Index memory goes to
4380+
* a scratch arena that dies with this call, as in cbm_run_php_lsp_cross. */
4381+
reg.index_first_registered = true;
4382+
CBMArena idx_arena;
4383+
cbm_arena_init(&idx_arena);
4384+
cbm_registry_finalize_into(&reg, &idx_arena);
4385+
43754386
/* Phase C: run the resolver. */
43764387
PHPLSPContext ctx;
43774388
php_lsp_init(&ctx, arena, source, source_len, &reg, module_qn, &result->resolved_calls);
@@ -4401,6 +4412,11 @@ void cbm_run_php_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
44014412
ctx.use_count = 0;
44024413
}
44034414

4415+
/* Trait flattening registered methods after the index was built; lookups
4416+
* would tail-scan them. Re-index once, first-registered as above, so the
4417+
* answers equal those of the indexed part followed by the tail scan. */
4418+
cbm_registry_finalize_into(&reg, &idx_arena);
4419+
44044420
php_lsp_process_file(&ctx, root);
44054421

44064422
if (ctx.debug) {
@@ -4414,6 +4430,7 @@ void cbm_run_php_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
44144430
rc->confidence);
44154431
}
44164432
}
4433+
cbm_arena_destroy(&idx_arena);
44174434
}
44184435

44194436
/* ── Cross-file + batch ───────────────────────────────────────── */

‎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);

‎internal/cbm/lsp/scope.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#ifdef CBM_ENABLE_TEST_SEAMS
77
_Thread_local uint64_t cbm_lsp_work_steps = 0;
8+
_Thread_local uint64_t cbm_lsp_work_ceiling = 0;
89
uint64_t cbm_lsp_work_take(void) {
910
uint64_t v = cbm_lsp_work_steps;
1011
cbm_lsp_work_steps = 0;

‎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
}

0 commit comments

Comments
 (0)