Skip to content

Commit 1661673

Browse files
committed
Merge remote-tracking branch 'origin/main' into fix/rust-generic-impl-call-scope
Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com> # Conflicts: # tests/test_pipeline.c
2 parents 9dc0ab6 + 1346ac4 commit 1661673

27 files changed

Lines changed: 2919 additions & 120 deletions

‎Makefile.cbm‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ TEST_PY_LSP_SCALE_SRCS = tests/test_py_lsp_scale.c
846846

847847
TEST_TS_LSP_SRCS = tests/test_ts_lsp.c
848848
TEST_JAVA_LSP_SRCS = tests/test_java_lsp.c tests/test_java_lsp_coverage.c
849-
TEST_KOTLIN_LSP_SRCS = tests/test_kotlin_lsp.c
849+
TEST_KOTLIN_LSP_SRCS = tests/test_kotlin_lsp.c tests/test_kotlin_typed_calls.c
850850
TEST_RUST_LSP_SRCS = tests/test_rust_lsp.c
851851

852852
TEST_INTEGRATION_SRCS = tests/test_integration.c tests/test_incremental.c tests/test_lang_contract.c tests/test_edge_imports.c tests/test_edge_structural.c tests/test_lsp_resolution_probe.c tests/test_node_creation_probe.c tests/test_edge_types_probe.c tests/test_convergence_probe.c tests/test_matrix_known_classes.c tests/test_matrix_new_constructs.c tests/test_grammar_probe_a.c tests/test_grammar_probe_b.c tests/test_grammar_probe_c.c tests/test_grammar_probe_d.c tests/test_grammar_probe_e.c tests/test_grammar_probe_f.c tests/test_grammar_probe_g.c tests/test_conditional_variants.c tests/test_spawns.c

‎internal/cbm/extract_defs.c‎

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6624,11 +6624,16 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
66246624
* scope. Note the call-scope side (compute_func_qn in extract_unified.c) is
66256625
* NOT language-gated — it qualifies whenever a scope is pushed — so this gate
66266626
* and the scope-push rule must move together, or a call QN names a def QN that
6627-
* was never minted and the edge is dropped at write. */
6627+
* was never minted and the edge is dropped at write.
6628+
*
6629+
* Python: the only enclosing scope that reaches here is the def a nested
6630+
* def sits in (py_push_def_body), so `def index()` inside create_app is
6631+
* proj.mod.create_app.index. */
66286632
if (ctx->enclosing_class_qn &&
66296633
(ctx->language == CBM_LANG_CPP || ctx->language == CBM_LANG_CUDA ||
66306634
ctx->language == CBM_LANG_TYPESCRIPT || ctx->language == CBM_LANG_TSX ||
6631-
ctx->language == CBM_LANG_ARKTS || ctx->language == CBM_LANG_NIX)) {
6635+
ctx->language == CBM_LANG_ARKTS || ctx->language == CBM_LANG_NIX ||
6636+
ctx->language == CBM_LANG_PYTHON)) {
66326637
def.qualified_name = cbm_arena_sprintf(a, "%s.%s", ctx->enclosing_class_qn, qn_name);
66336638
}
66346639
def.label = "Function";
@@ -11667,6 +11672,51 @@ static void push_class_body_children(TSNode node, const CBMLangSpec *spec, wd_st
1166711672
}
1166811673
}
1166911674

11675+
/* Python: a def's body is a scope of its own, and a `def` or `class` statement
11676+
* in it is a definition like any other -- closures, decorator wrappers, the
11677+
* route handlers of an app factory (`@app.route` inside create_app), local
11678+
* classes. Walk the body with the def's QN as the enclosing scope, so a nested
11679+
* def is qualified by the def it sits in: proj.mod.create_app.index. That is
11680+
* the QN the Python LSP already gives the calls inside it (py_process_function)
11681+
* and the one the call-scope walk computes (extract_unified.c
11682+
* py_nearest_scope_qn). Only the body is walked: parameters,
11683+
* annotations and decorators hold expressions, never statements. A lambda or
11684+
* a comprehension holds no statements either, so neither becomes a def. */
11685+
static void py_push_def_body(wd_stack_t *s, TSNode def_node, const char *def_qn) {
11686+
TSNode body = ts_node_child_by_field_name(def_node, TS_FIELD("body"));
11687+
if (def_qn && !ts_node_is_null(body)) {
11688+
wd_push(s, body, def_qn);
11689+
}
11690+
}
11691+
11692+
/* Python methods are extracted by extract_class_methods, not by the walk, so
11693+
* their bodies are pushed here, under the QN push_method_def gave the method
11694+
* (class_qn.name), in source order. */
11695+
static void py_push_method_bodies(CBMExtractCtx *ctx, TSNode class_node, const char *class_qn,
11696+
wd_stack_t *s) {
11697+
TSNode body = find_class_member_body(class_node, ctx->language);
11698+
if (ts_node_is_null(body) || !class_qn) {
11699+
return;
11700+
}
11701+
for (int i = (int)ts_node_named_child_count(body) - SKIP_CHAR; i >= 0; i--) {
11702+
TSNode method = ts_node_named_child(body, (uint32_t)i);
11703+
if (strcmp(ts_node_type(method), "decorated_definition") == 0) {
11704+
method = ts_node_child_by_field_name(method, TS_FIELD("definition"));
11705+
}
11706+
if (ts_node_is_null(method) || strcmp(ts_node_type(method), "function_definition") != 0) {
11707+
continue;
11708+
}
11709+
TSNode name_node = resolve_method_name(method, ctx->language);
11710+
char *name =
11711+
ts_node_is_null(name_node)
11712+
? NULL
11713+
: cbm_func_name_node_text(ctx->arena, name_node, ctx->source, ctx->language);
11714+
if (name && name[0]) {
11715+
py_push_def_body(s, method, cbm_arena_sprintf(ctx->arena, "%s.%s", class_qn, name));
11716+
}
11717+
}
11718+
}
11719+
1167011720
// ASCII case-insensitive equality vs a lowercase literal (CFML attribute names
1167111721
// are case-insensitive, e.g. NAME / Name / name).
1167211722
static bool ascii_ci_equals(const char *s, const char *lower_lit) {
@@ -12211,6 +12261,16 @@ static void walk_defs(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec,
1221112261
}
1221212262
if (cbm_kind_in_set(node, spec->function_node_types)) {
1221312263
if (!is_template_class_node(node, ctx->language)) {
12264+
if (ctx->language == CBM_LANG_PYTHON) {
12265+
int defs_before = ctx->result->defs.count;
12266+
extract_func_def(ctx, node, spec);
12267+
if (ctx->result->defs.count > defs_before) {
12268+
py_push_def_body(&s, node,
12269+
ctx->result->defs.items[ctx->result->defs.count - SKIP_ONE]
12270+
.qualified_name);
12271+
}
12272+
continue;
12273+
}
1221412274
extract_func_def(ctx, node, spec);
1221512275
// Most languages stop here. JS/TS (and Wolfram) descend into the
1221612276
// function body so NESTED named definitions are also captured —
@@ -12268,6 +12328,9 @@ static void walk_defs(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec,
1226812328
extract_class_def(ctx, node, spec);
1226912329
const char *new_enclosing = compute_class_qn(ctx, node, frame.enclosing_class_qn);
1227012330
push_class_body_children(node, spec, &s, new_enclosing, ctx);
12331+
if (ctx->language == CBM_LANG_PYTHON && new_enclosing != frame.enclosing_class_qn) {
12332+
py_push_method_bodies(ctx, node, new_enclosing, &s);
12333+
}
1227112334
continue;
1227212335
}
1227312336

‎internal/cbm/extract_unified.c‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,21 @@ static const char *objectscript_get_method_qn(CBMExtractCtx *ctx, TSNode node,
916916
return NULL;
917917
}
918918

919+
/* Python: the nearest named scope around the node -- a def or a class. A def
920+
* or class nested in a def is qualified by that def (proj.mod.create_app.index),
921+
* exactly as extract_defs.c mints it; enclosing_class_qn alone would skip the
922+
* def and hand a def nested in a method its method's QN. Lambdas push no named
923+
* frame, so they stay transparent. */
924+
static const char *py_nearest_scope_qn(const WalkState *state) {
925+
for (int i = state->scope_top - SKIP_ONE; i >= 0; i--) {
926+
const CBMWalkScope *f = &state->scopes[i];
927+
if ((f->kind == SCOPE_FUNC || f->kind == SCOPE_CLASS) && f->qn) {
928+
return f->qn;
929+
}
930+
}
931+
return NULL;
932+
}
933+
919934
// Compute function QN for scope tracking (mirrors cbm_enclosing_func_qn logic).
920935
static const char *compute_func_qn(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec,
921936
WalkState *state) {
@@ -1107,8 +1122,10 @@ static const char *compute_func_qn(CBMExtractCtx *ctx, TSNode node, const CBMLan
11071122
}
11081123
}
11091124

1110-
if (state->enclosing_class_qn) {
1111-
return cbm_arena_sprintf(ctx->arena, "%s.%s", state->enclosing_class_qn, qn_name);
1125+
const char *outer_qn =
1126+
ctx->language == CBM_LANG_PYTHON ? py_nearest_scope_qn(state) : state->enclosing_class_qn;
1127+
if (outer_qn) {
1128+
return cbm_arena_sprintf(ctx->arena, "%s.%s", outer_qn, qn_name);
11121129
}
11131130
/* Java/Go: directory-based module so this enclosing-func QN matches the def
11141131
* QN and the LSP caller_qn (the lsp_resolve join keys on exact equality). */
@@ -1176,9 +1193,15 @@ static const char *compute_class_qn(CBMExtractCtx *ctx, TSNode node, const WalkS
11761193

11771194
/* Nested class: prefix with the enclosing class QN (Outer.Inner) so this
11781195
* scope QN matches the def-side class QN (extract_defs.c compute_class_qn /
1179-
* extract_class_def), which the lsp_resolve join requires for nested types. */
1180-
if (state && state->enclosing_class_qn) {
1181-
return cbm_arena_sprintf(ctx->arena, "%s.%s", state->enclosing_class_qn, name);
1196+
* extract_class_def), which the lsp_resolve join requires for nested types.
1197+
* Python: a class inside a def is qualified by the def (py_nearest_scope_qn). */
1198+
const char *outer_qn = NULL;
1199+
if (state) {
1200+
outer_qn = ctx->language == CBM_LANG_PYTHON ? py_nearest_scope_qn(state)
1201+
: state->enclosing_class_qn;
1202+
}
1203+
if (outer_qn) {
1204+
return cbm_arena_sprintf(ctx->arena, "%s.%s", outer_qn, name);
11821205
}
11831206

11841207
/* Java/Go: directory-based module (see compute_func_qn). */

0 commit comments

Comments
 (0)