Skip to content

Commit 78aea85

Browse files
committed
fix(python): resolve class-body and decorator calls
Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 82c2b83 commit 78aea85

4 files changed

Lines changed: 863 additions & 14 deletions

File tree

‎internal/cbm/lsp/py_lsp.c‎

Lines changed: 230 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
static _Thread_local int py_test_memo_allocations_left = -1;
4545
static _Thread_local bool py_test_depth_failure;
4646
static _Thread_local int py_test_scope_allocations_left = -1;
47+
static _Thread_local int py_test_class_frames_left = -1;
48+
void cbm_py_lsp_test_class_frame_fail_after(int successful_allocations) {
49+
py_test_class_frames_left = successful_allocations;
50+
}
4751
void cbm_py_lsp_test_scope_fail_after(int successful_allocations) {
4852
py_test_scope_allocations_left = successful_allocations;
4953
}
@@ -227,6 +231,7 @@ static void py_resolve_calls_in(PyLSPContext *ctx, TSNode node) {
227231
static const CBMType *py_eval_expr_type(PyLSPContext *ctx, TSNode node);
228232
static const CBMType *py_eval_expr_type_uncached(PyLSPContext *ctx, TSNode node);
229233
static void py_process_statement(PyLSPContext *ctx, TSNode node);
234+
static void py_exec_class_definition(PyLSPContext *ctx, TSNode class_node);
230235
static void py_invalidate_possible_bindings(PyLSPContext *ctx, TSNode node, int depth);
231236
static const CBMRegisteredFunc *py_lookup_attribute(PyLSPContext *ctx, const char *type_qn,
232237
const char *member_name);
@@ -288,6 +293,25 @@ static CBMScope *py_scope_push_checked(PyLSPContext *ctx) {
288293
return child;
289294
}
290295

296+
/* New class execution frames must never fall back to their parent: body
297+
* bindings would escape and subsequent calls could use those stale bindings. */
298+
static CBMScope *py_class_frame_push(PyLSPContext *ctx, CBMScope *parent) {
299+
#ifdef CBM_ENABLE_TEST_SEAMS
300+
if (py_test_class_frames_left == 0) {
301+
ctx->eval_failure = CBM_LSP_SCOPE_FAILED;
302+
return NULL;
303+
}
304+
if (py_test_class_frames_left > 0)
305+
py_test_class_frames_left--;
306+
#endif
307+
CBMScope *frame = cbm_scope_push(ctx->arena, parent);
308+
if (!frame || frame == parent) {
309+
ctx->eval_failure = CBM_LSP_SCOPE_FAILED;
310+
return NULL;
311+
}
312+
return frame;
313+
}
314+
291315
static void py_scope_clear_callable(PyLSPContext *ctx, const char *name) {
292316
if (cbm_scope_update_callable(ctx->current_scope, name, NULL)) {
293317
ctx->type_cache_gen++;
@@ -2565,13 +2589,16 @@ static void py_process_statement(PyLSPContext *ctx, TSNode node) {
25652589
} else {
25662590
py_scope_bind(ctx, name, rhs_type);
25672591
}
2592+
// Class attributes must not replace file-wide dispatch entries.
25682593
// Lambda registry: `fn = lambda x: ...`.
2569-
if (!ts_node_is_null(right) && strcmp(ts_node_type(right), "lambda") == 0) {
2594+
if (!ctx->class_body_outer_scope && !ts_node_is_null(right) &&
2595+
strcmp(ts_node_type(right), "lambda") == 0) {
25702596
py_register_lambda(ctx, name, right);
25712597
}
25722598
// Dict literal dispatch table: `funcs = {"a": foo, "b": bar}`
25732599
// with all values being known function QNs.
2574-
if (!ts_node_is_null(right) && strcmp(ts_node_type(right), "dictionary") == 0) {
2600+
if (!ctx->class_body_outer_scope && !ts_node_is_null(right) &&
2601+
strcmp(ts_node_type(right), "dictionary") == 0) {
25752602
py_register_dict_literal(ctx, name, right);
25762603
}
25772604
}
@@ -2779,7 +2806,11 @@ static void py_emit_call_for(PyLSPContext *ctx, TSNode call_node) {
27792806
// Walk the lambda body for any nested call sites with the params
27802807
// bound to the call's arg types. Resolved calls get caller_qn
27812808
// set to a synthetic <lambda> child of the enclosing function.
2782-
TSNode lambda_node = py_lookup_lambda(ctx, fname);
2809+
/* These legacy entries have no lexical frame identity. During class
2810+
* execution, leave lambda replay unresolved instead of bypassing a
2811+
* local shadow or running the lambda against class locals. */
2812+
TSNode lambda_node =
2813+
ctx->class_body_outer_scope ? (TSNode){0} : py_lookup_lambda(ctx, fname);
27832814
if (!ts_node_is_null(lambda_node)) {
27842815
TSNode params = ts_node_child_by_field_name(lambda_node, "parameters", 10);
27852816
TSNode body = ts_node_child_by_field_name(lambda_node, "body", 4);
@@ -2890,7 +2921,7 @@ static void py_emit_call_for(PyLSPContext *ctx, TSNode call_node) {
28902921

28912922
// Subscript-as-call: `funcs["a"]()` where funcs is a registered
28922923
// dict-literal dispatch table.
2893-
if (strcmp(fk, "subscript") == 0) {
2924+
if (!ctx->class_body_outer_scope && strcmp(fk, "subscript") == 0) {
28942925
TSNode container = ts_node_child_by_field_name(fn, "value", 5);
28952926
TSNode key = ts_node_child_by_field_name(fn, "subscript", 9);
28962927
if (!ts_node_is_null(container) && !ts_node_is_null(key) &&
@@ -3465,6 +3496,65 @@ static void py_emit_dunder_call(PyLSPContext *ctx, const CBMType *recv, const ch
34653496
}
34663497
}
34673498

3499+
/* A class comprehension evaluates its leftmost iterable in the class frame,
3500+
* but the implicit body scope skips class locals. Nested comprehensions keep
3501+
* the surrounding comprehension frame. Keep class execution marked active so
3502+
* legacy bare-name dispatch cannot bypass either frame's lexical bindings. */
3503+
static void py_resolve_class_comprehension(PyLSPContext *ctx, TSNode node) {
3504+
if (ctx->eval_failure)
3505+
return;
3506+
PyKids kids = py_kids(ctx->arena, node);
3507+
uint32_t first_for = kids.n;
3508+
TSNode first_right = {0};
3509+
for (uint32_t i = 0; i < kids.n && !ctx->eval_failure; i++) {
3510+
TSNode child = py_kid(&kids, i);
3511+
if (strcmp(ts_node_type(child), "for_in_clause") == 0) {
3512+
first_for = i;
3513+
first_right = ts_node_child_by_field_name(child, "right", 5);
3514+
break;
3515+
}
3516+
}
3517+
if (first_for == kids.n)
3518+
return;
3519+
const CBMType *first_type = py_eval_expr_type(ctx, first_right);
3520+
py_resolve_calls_in(ctx, first_right);
3521+
if (ctx->eval_failure)
3522+
return;
3523+
const CBMType *first_elem = py_iterable_element_type(ctx, first_type);
3524+
CBMScope *saved_scope = ctx->current_scope;
3525+
bool saved_comprehension = ctx->class_body_in_comprehension;
3526+
CBMScope *parent = saved_comprehension ? saved_scope : ctx->class_body_outer_scope;
3527+
CBMScope *frame = py_class_frame_push(ctx, parent);
3528+
if (!frame)
3529+
return;
3530+
py_scope_restore(ctx, frame);
3531+
ctx->class_body_in_comprehension = true;
3532+
/* Clauses precede the body at evaluation time, although tree-sitter stores
3533+
* the body first. Walk each iterable/filter only once, before later binds. */
3534+
for (uint32_t i = first_for; i < kids.n && !ctx->eval_failure; i++) {
3535+
TSNode child = py_kid(&kids, i);
3536+
if (strcmp(ts_node_type(child), "for_in_clause") == 0) {
3537+
TSNode left = ts_node_child_by_field_name(child, "left", 4);
3538+
const CBMType *elem = first_elem;
3539+
if (i != first_for) {
3540+
TSNode right = ts_node_child_by_field_name(child, "right", 5);
3541+
const CBMType *iter = py_eval_expr_type(ctx, right);
3542+
py_resolve_calls_in(ctx, right);
3543+
if (ctx->eval_failure)
3544+
break;
3545+
elem = py_iterable_element_type(ctx, iter);
3546+
}
3547+
py_bind_for_target(ctx, left, elem);
3548+
} else {
3549+
py_resolve_calls_in(ctx, child);
3550+
}
3551+
}
3552+
for (uint32_t i = 0; i < first_for && !ctx->eval_failure; i++)
3553+
py_resolve_calls_in(ctx, py_kid(&kids, i));
3554+
ctx->class_body_in_comprehension = saved_comprehension;
3555+
py_scope_restore(ctx, saved_scope);
3556+
}
3557+
34683558
static void py_resolve_calls_in_inner(PyLSPContext *ctx, TSNode node) {
34693559
if (!ctx || ts_node_is_null(node))
34703560
return;
@@ -3667,6 +3757,10 @@ static void py_resolve_calls_in_inner(PyLSPContext *ctx, TSNode node) {
36673757
// iterable's element type, then walk inner expressions.
36683758
if (strcmp(k, "list_comprehension") == 0 || strcmp(k, "dictionary_comprehension") == 0 ||
36693759
strcmp(k, "set_comprehension") == 0 || strcmp(k, "generator_expression") == 0) {
3760+
if (ctx->class_body_outer_scope) {
3761+
py_resolve_class_comprehension(ctx, node);
3762+
return;
3763+
}
36703764
CBMScope *saved = ctx->current_scope;
36713765
ctx->current_scope = py_scope_push_checked(ctx);
36723766
uint32_t cnc = ts_node_named_child_count(node);
@@ -3699,15 +3793,19 @@ static void py_resolve_calls_in_inner(PyLSPContext *ctx, TSNode node) {
36993793
// Recurse: children. We don't push scope for control-flow blocks
37003794
// here (Python scoping is function-level apart from comprehension /
37013795
// lambda / class), but we do for nested function / class / lambda.
3702-
if (strcmp(k, "function_definition") == 0 || strcmp(k, "class_definition") == 0 ||
3703-
strcmp(k, "lambda") == 0) {
3796+
if (strcmp(k, "function_definition") == 0 || strcmp(k, "lambda") == 0) {
37043797
// These are processed by the top-level pass; skip recursion to
37053798
// avoid double-walking their bodies.
37063799
return;
37073800
}
37083801

3802+
if (strcmp(k, "class_definition") == 0) {
3803+
py_exec_class_definition(ctx, node);
3804+
return;
3805+
}
3806+
37093807
PyKids nk = py_kids(ctx->arena, node);
3710-
for (uint32_t i = 0; i < nk.n; i++) {
3808+
for (uint32_t i = 0; i < nk.n && !ctx->eval_failure; i++) {
37113809
py_resolve_calls_in(ctx, py_kid(&nk, i));
37123810
}
37133811
}
@@ -4328,6 +4426,120 @@ static bool py_is_init_method(PyLSPContext *ctx, TSNode func_node) {
43284426
return nm && (strcmp(nm, "__init__") == 0 || strcmp(nm, "__post_init__") == 0);
43294427
}
43304428

4429+
/* Decorator expressions of a decorated definition, in source order. They
4430+
* evaluate in the scope enclosing the definition, before it is bound. A bare
4431+
* `@name` is no call and emits nothing; `@name(args)` is one. */
4432+
static void py_resolve_decorators(PyLSPContext *ctx, TSNode decorated) {
4433+
if (!ctx || ctx->eval_failure)
4434+
return;
4435+
PyKids kids = py_kids(ctx->arena, decorated);
4436+
for (uint32_t i = 0; i < kids.n && !ctx->eval_failure; i++) {
4437+
TSNode child = py_kid(&kids, i);
4438+
if (strcmp(ts_node_type(child), "decorator") == 0)
4439+
py_resolve_calls_in(ctx, child);
4440+
}
4441+
}
4442+
4443+
/* Bind the name a class-body `def`/`class` statement defines in the class
4444+
* frame, so later class-body statements see it (and never the same-named
4445+
* module symbol). Its value is exact only for an undecorated function or a
4446+
* registered class whose QN is known; anything else is an UNKNOWN shadow. */
4447+
static void py_bind_class_body_def(PyLSPContext *ctx, TSNode stmt) {
4448+
if (ctx->eval_failure)
4449+
return;
4450+
TSNode def = stmt;
4451+
bool decorated = strcmp(ts_node_type(stmt), "decorated_definition") == 0;
4452+
if (decorated) {
4453+
def = ts_node_child_by_field_name(stmt, "definition", 10);
4454+
if (ts_node_is_null(def))
4455+
return;
4456+
}
4457+
const char *kind = ts_node_type(def);
4458+
bool is_class = strcmp(kind, "class_definition") == 0;
4459+
if (!is_class && strcmp(kind, "function_definition") != 0)
4460+
return;
4461+
char *name = py_node_text(ctx, ts_node_child_by_field_name(def, "name", 4));
4462+
if (!name || !name[0]) {
4463+
py_disable_callable_value_proof(ctx);
4464+
return;
4465+
}
4466+
const char *qn = ctx->class_body_qn
4467+
? cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->class_body_qn, name)
4468+
: NULL;
4469+
if (is_class) {
4470+
const CBMRegisteredType *type = qn ? cbm_registry_lookup_type(ctx->registry, qn) : NULL;
4471+
py_scope_bind(ctx, name,
4472+
type ? cbm_type_named(ctx->arena, type->qualified_name) : cbm_type_unknown());
4473+
return;
4474+
}
4475+
const CBMRegisteredFunc *func = qn ? cbm_registry_lookup_func(ctx->registry, qn) : NULL;
4476+
if (!decorated && py_func_is_exact_callable_value(func)) {
4477+
py_scope_bind_callable(ctx, name, cbm_type_unknown(), func->qualified_name);
4478+
} else {
4479+
py_scope_bind(ctx, name, cbm_type_unknown());
4480+
}
4481+
}
4482+
4483+
/* Execute a class definition where it stands: its bases in the enclosing
4484+
* scope, then its body statements in source order in a class frame. Calls
4485+
* keep the caller the extraction gives them: the enclosing function, or the
4486+
* module QN at module level (enclosing_func_qn is left unchanged).
4487+
*
4488+
* Scoping follows Python: a name bound earlier in the body is visible to
4489+
* later body statements (and to decorators of methods), but the class frame
4490+
* is not an enclosing scope for anything nested in it. Method bodies are
4491+
* resolved later by py_process_class against the module scope, and a nested
4492+
* class body chains to the nearest enclosing non-class scope. Each body
4493+
* statement is walked exactly once. */
4494+
static void py_exec_class_definition(PyLSPContext *ctx, TSNode class_node) {
4495+
if (!ctx || ctx->eval_failure || ts_node_is_null(class_node))
4496+
return;
4497+
TSNode supers = ts_node_child_by_field_name(class_node, "superclasses", 12);
4498+
if (!ts_node_is_null(supers))
4499+
py_resolve_calls_in(ctx, supers);
4500+
TSNode body = ts_node_child_by_field_name(class_node, "body", 4);
4501+
if (ctx->eval_failure || ts_node_is_null(body))
4502+
return;
4503+
4504+
char *cname = py_node_text(ctx, ts_node_child_by_field_name(class_node, "name", 4));
4505+
const char *class_qn = NULL;
4506+
if (cname && cname[0]) {
4507+
if (ctx->class_body_outer_scope) {
4508+
class_qn = ctx->class_body_qn
4509+
? cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->class_body_qn, cname)
4510+
: NULL;
4511+
} else if (ctx->module_qn && ctx->enclosing_func_qn && ctx->file_module_qn &&
4512+
strcmp(ctx->enclosing_func_qn, ctx->file_module_qn) == 0) {
4513+
class_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->module_qn, cname);
4514+
}
4515+
}
4516+
4517+
CBMScope *saved_scope = ctx->current_scope;
4518+
CBMScope *saved_outer = ctx->class_body_outer_scope;
4519+
const char *saved_qn = ctx->class_body_qn;
4520+
bool saved_comprehension = ctx->class_body_in_comprehension;
4521+
CBMScope *outer = saved_outer ? saved_outer : saved_scope;
4522+
CBMScope *frame = py_class_frame_push(ctx, outer);
4523+
if (!frame)
4524+
return;
4525+
py_scope_restore(ctx, frame);
4526+
ctx->class_body_outer_scope = outer;
4527+
ctx->class_body_qn = class_qn;
4528+
ctx->class_body_in_comprehension = false;
4529+
4530+
PyKids bk = py_kids(ctx->arena, body);
4531+
for (uint32_t i = 0; i < bk.n && !ctx->eval_failure; i++) {
4532+
TSNode stmt = py_kid(&bk, i);
4533+
py_resolve_calls_in(ctx, stmt);
4534+
py_bind_class_body_def(ctx, stmt);
4535+
}
4536+
4537+
ctx->class_body_outer_scope = saved_outer;
4538+
ctx->class_body_qn = saved_qn;
4539+
ctx->class_body_in_comprehension = saved_comprehension;
4540+
py_scope_restore(ctx, saved_scope);
4541+
}
4542+
43314543
static void py_process_class(PyLSPContext *ctx, TSNode class_node) {
43324544
if (!ctx || ctx->eval_failure)
43334545
return;
@@ -4990,15 +5202,21 @@ void py_lsp_process_file(PyLSPContext *ctx, TSNode root) {
49905202
} else if (strcmp(ck, "function_definition") == 0) {
49915203
py_bind_module_function(ctx, c);
49925204
} else if (strcmp(ck, "class_definition") == 0) {
4993-
py_bind_module_class(ctx, c);
5205+
py_exec_class_definition(ctx, c);
5206+
if (!ctx->eval_failure)
5207+
py_bind_module_class(ctx, c);
49945208
} else if (strcmp(ck, "decorated_definition") == 0) {
5209+
py_resolve_decorators(ctx, c);
49955210
TSNode def = ts_node_child_by_field_name(c, "definition", 10);
4996-
if (!ts_node_is_null(def)) {
5211+
if (!ctx->eval_failure && !ts_node_is_null(def)) {
49975212
const char *dk = ts_node_type(def);
4998-
if (strcmp(dk, "function_definition") == 0)
5213+
if (strcmp(dk, "function_definition") == 0) {
49995214
py_bind_module_function(ctx, def);
5000-
else if (strcmp(dk, "class_definition") == 0)
5001-
py_bind_module_class(ctx, def);
5215+
} else if (strcmp(dk, "class_definition") == 0) {
5216+
py_exec_class_definition(ctx, def);
5217+
if (!ctx->eval_failure)
5218+
py_bind_module_class(ctx, def);
5219+
}
50025220
}
50035221
} else if (strcmp(ck, "expression_statement") == 0) {
50045222
/* An expression statement is resolved for calls, not scanned as a

‎internal/cbm/lsp/py_lsp.h‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,13 @@ typedef struct {
6161
// caller QNs.
6262
const char *enclosing_func_qn;
6363
const char *enclosing_class_qn;
64-
const char *module_qn; // Python symbol scope (package __init__ omitted)
65-
const char *file_module_qn; // raw file identity; borrowed for this API call
64+
// Active class execution: nested class bodies skip this class frame.
65+
// Local classes have no extracted graph QN, so class_body_qn stays NULL.
66+
CBMScope *class_body_outer_scope;
67+
const char *class_body_qn;
68+
bool class_body_in_comprehension; // retain enclosing comprehension bindings
69+
const char *module_qn; // Python symbol scope (package __init__ omitted)
70+
const char *file_module_qn; // raw file identity; borrowed for this API call
6671

6772
// Output: resolved calls accumulate here.
6873
CBMResolvedCallArray *resolved_calls;
@@ -180,6 +185,8 @@ void cbm_py_lsp_record_failure(CBMFileResult *result, CBMLSPStatus status);
180185
void cbm_py_lsp_test_memo_fail_after(int successful_allocations);
181186
void cbm_py_lsp_test_depth_fail(bool enabled);
182187
void cbm_py_lsp_test_scope_fail_after(int successful_allocations);
188+
// Separate from the package symbol-scope seam; only new class execution frames.
189+
void cbm_py_lsp_test_class_frame_fail_after(int successful_allocations);
183190
bool cbm_py_lsp_test_register_defs(CBMArena *arena, CBMTypeRegistry *reg, CBMLSPDef *defs,
184191
int def_count);
185192
const CBMType *cbm_py_lsp_test_eval(PyLSPContext *ctx, TSNode node);

0 commit comments

Comments
 (0)