diff --git a/internal/cbm/lsp/scope.c b/internal/cbm/lsp/scope.c index c44a0b544..2d9740db0 100644 --- a/internal/cbm/lsp/scope.c +++ b/internal/cbm/lsp/scope.c @@ -88,20 +88,6 @@ bool cbm_scope_bind_callable_checked(CBMScope *scope, const char *name, const CB return cbm_scope_bind_value(scope, name, type, callable_qn); } -const CBMVarBinding *cbm_scope_lookup_binding(const CBMScope *scope, const char *name) { - if (!name) - return NULL; - for (const CBMScope* s = scope; s != NULL; s = s->parent) { - for (const 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]; - } - } - } - return NULL; -} - const CBMType* cbm_scope_lookup(const CBMScope* scope, const char* name) { const CBMVarBinding *binding = cbm_scope_lookup_binding(scope, name); if (binding) diff --git a/internal/cbm/lsp/scope.h b/internal/cbm/lsp/scope.h index 3ec438fe9..5ba7265a3 100644 --- a/internal/cbm/lsp/scope.h +++ b/internal/cbm/lsp/scope.h @@ -5,6 +5,7 @@ #include "../arena.h" #include /* relaxed cache for cbm_lsp_max_walk_depth */ #include /* getenv, atoi (cbm_lsp_max_walk_depth) */ +#include /* strcmp (cbm_scope_lookup_binding) */ typedef struct { const char* name; @@ -30,6 +31,25 @@ typedef struct CBMScope { CBMArena* arena; // owning arena, propagated to children at push time } CBMScope; +/* Return the complete nearest binding in one scope-chain walk, or NULL when + * unbound. Keep this internal hot-path primitive inline: all language + * resolvers use cbm_scope_lookup(), while Python also consumes the complete + * record to avoid repeating the same linear scan. */ +static inline const CBMVarBinding *cbm_scope_lookup_binding(const CBMScope *scope, + const char *name) { + if (!name) + return NULL; + for (const CBMScope *s = scope; s != NULL; s = s->parent) { + for (const 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]; + } + } + } + return NULL; +} + // Bail-to-UNKNOWN depth for type-lookup chains: alias resolution, MRO walks, // embedded-field/struct-traversal. Exceeding this collapses to cbm_type_unknown // rather than recursing — guards against pathological hierarchies. @@ -82,10 +102,6 @@ bool cbm_scope_bind_callable_checked(CBMScope *scope, const char *name, const CB void cbm_scope_bind_callable(CBMScope *scope, const char *name, const CBMType *type, const char *callable_qn); const CBMType* cbm_scope_lookup(const CBMScope* scope, const char* name); -/* Return the complete nearest binding in one scope-chain walk, or NULL when - * unbound. Callers that need presence, type, and callable identity together - * should use this instead of repeating three linear scans. */ -const CBMVarBinding *cbm_scope_lookup_binding(const CBMScope *scope, const char *name); /* True when any lexical frame contains name, even when its type is UNKNOWN. */ bool cbm_scope_contains(const CBMScope *scope, const char *name); /* Return the exact callable QN from the nearest binding. A nearer ordinary