Files
Martin Vogel 6757dc8c0c refactor(lsp): chunked linked-frame CBMScope + MRO depth cap
Phase 0 of Python LSP integration. Two shared-infrastructure fixes that
silently bug Python without affecting Go/C++ regression tests:

- CBMScope replaces the fixed 64-binding array with a growable linked
  list of 16-binding chunks. The arena pointer is captured at scope_push
  and reused for chunk allocation, so existing callers benefit from
  dynamic growth without an API change. Python class bodies and large
  dataclass scopes routinely exceed 64 locals; this prevents silent
  drops past the legacy cap.

- CBM_LSP_MAX_LOOKUP_DEPTH=16 wraps go_lookup_field_or_method and
  c_lookup_member via depth-aware internal helpers. Pathological
  multi-inheritance hierarchies (SQLAlchemy / Django / Zope) can exceed
  20 levels and previously stack-overflowed; now they degrade to NULL
  past depth 16 instead.

Adds tests/test_scope.c with 10 cases covering basic API, parent-chain
walks, child-shadowing, and dynamic growth at 300 bindings + chunk
boundaries. test_main.c and Makefile.cbm wire the new suite.

PYTHON_LSP_PLAN.md added at the worktree root: full phased plan plus
the verbatim challenger review.

All 2823 existing tests stay green; 10 new scope tests pass.
2026-05-08 22:26:56 +02:00

75 lines
2.0 KiB
C

#include "scope.h"
#include <string.h>
CBMScope* cbm_scope_push(CBMArena* a, CBMScope* current) {
CBMScope* scope = (CBMScope*)cbm_arena_alloc(a, sizeof(CBMScope));
if (!scope) {
return current;
}
memset(scope, 0, sizeof(CBMScope));
scope->parent = current;
scope->arena = a;
return scope;
}
CBMScope* cbm_scope_pop(CBMScope* scope) {
if (!scope) {
return NULL;
}
return scope->parent;
}
static CBMScopeChunk* alloc_chunk(CBMScope* scope) {
if (!scope->arena) {
return NULL;
}
CBMScopeChunk* c = (CBMScopeChunk*)cbm_arena_alloc(scope->arena, sizeof(CBMScopeChunk));
if (!c) {
return NULL;
}
memset(c, 0, sizeof(CBMScopeChunk));
c->next = scope->chunks;
scope->chunks = c;
return c;
}
void cbm_scope_bind(CBMScope* scope, const char* name, const CBMType* type) {
if (!scope || !name) {
return;
}
for (CBMScopeChunk* c = scope->chunks; c != NULL; c = c->next) {
for (int i = 0; i < c->used; i++) {
if (c->bindings[i].name && strcmp(c->bindings[i].name, name) == 0) {
c->bindings[i].type = type;
return;
}
}
}
CBMScopeChunk* head = scope->chunks;
if (!head || head->used >= CBM_SCOPE_CHUNK_BINDINGS) {
head = alloc_chunk(scope);
if (!head) {
return;
}
}
head->bindings[head->used].name = name;
head->bindings[head->used].type = type;
head->used++;
}
const CBMType* cbm_scope_lookup(const CBMScope* scope, const char* name) {
if (!name) {
return cbm_type_unknown();
}
for (const CBMScope* s = scope; s != NULL; s = s->parent) {
for (CBMScopeChunk* c = s->chunks; c != NULL; c = c->next) {
for (int i = 0; i < c->used; i++) {
if (c->bindings[i].name && strcmp(c->bindings[i].name, name) == 0) {
return c->bindings[i].type;
}
}
}
}
return cbm_type_unknown();
}