Skip to content

Commit 0bd65aa

Browse files
committed
feat(extract): mint Variable nodes for Nix module-level bindings
`nix_var_types` has declared `binding` since the language was added, but no Nix case existed in extract_var_names and no path reached a binding in the first place, so the Variable count for Nix was unconditionally zero. Two things were in the way. extract_variables iterates only the file root's DIRECT children, and a Nix file's root child is its header lambda (`{ pkgs, lib, ... }:`), so the generic loop saw nothing — the same shape as the definition bug. And extract_var_names had no Nix branch, so a binding could not have been named even if reached. Scope follows the rule the other languages already use rather than inventing one. extract_variables mints FILE scope and never locals: a C++ declaration inside a function body is not a Variable. For Nix, file scope is the `let` bindings and the returned attrset — a let binding is file scope in the same sense a C++ file-static is, and the attrset is the exported surface. A binding in a deeper attrset is not. That bound is load-bearing. Every Nix binding's parent is a binding_set at any depth, so admitting them all would mint a node per `enable = true` in a NixOS module's settings tree — the per-leaf flood the Helm values.yaml case in this same function already exists to avoid. Bindings whose value is a lambda or an attribute set are skipped: the first is already minted as a Function by the def walk, the second is a scope (is_namespace_scope_kind), so minting either here would double-count it. Names follow the attrpath convention from the previous commit — leaf as the name, whole path in the qualified name, so `services.nginx.enable = true` is name `enable`, QN proj.file.services.nginx.enable. push_var_def grows a _qn variant for that; the plain form delegates to it and is unchanged for every other language. Extraction and pipeline suites: 494 passed, 0 failed. Signed-off-by: Jason Bowman <jason@json64.dev>
1 parent b5209af commit 0bd65aa

1 file changed

Lines changed: 117 additions & 3 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ enum {
3131

3232
EXPORT_ANCESTOR_DEPTH = 4,
3333
FUNC_PARENT_CLIMB_LIMIT = 4, /* fun_expr -> term -> uni_term -> let_binding (Nickel) */
34+
/* Nix header lambdas to descend before the file's body: `{ pkgs, ... }:` is one,
35+
* the nixpkgs overlay `final: prev:` is two. Bounded so a pathological chain
36+
* cannot spin. */
37+
NIX_HEADER_HOP_MAX = 8,
3438
DECORATOR_SCAN_LIMIT = 3,
3539
C_RETURN_WALK_DEPTH = 5,
3640
VAR_RECURSION_LIMIT = 8,
@@ -188,6 +192,7 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
188192
static void extract_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec);
189193
static void walk_defs(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec, int depth_unused);
190194
static void extract_variables(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec);
195+
static void extract_var_names(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec);
191196
static void extract_class_variables(CBMExtractCtx *ctx, TSNode class_node, const CBMLangSpec *spec);
192197
static void extract_rust_impl(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec);
193198
static void extract_class_methods(CBMExtractCtx *ctx, TSNode class_node, const char *class_qn,
@@ -4751,7 +4756,12 @@ static void extract_elixir_call(CBMExtractCtx *ctx, TSNode node, const CBMLangSp
47514756
// --- Variable extraction ---
47524757

47534758
// Helper to push a Variable definition
4754-
static void push_var_def(CBMExtractCtx *ctx, const char *name, TSNode node) {
4759+
/* `qn_name` is the name as it should appear in the qualified name, which differs
4760+
* from `name` only where a language scopes a variable below the module — Nix,
4761+
* whose binding names are attrpaths (`a.b.c = …` is name `c`, QN suffix `a.b.c`).
4762+
* Pass NULL to use `name` for both. */
4763+
static void push_var_def_qn(CBMExtractCtx *ctx, const char *name, const char *qn_name,
4764+
TSNode node) {
47554765
if (!name || !name[0] || strcmp(name, "_") == 0) {
47564766
return;
47574767
}
@@ -4761,8 +4771,8 @@ static void push_var_def(CBMExtractCtx *ctx, const char *name, TSNode node) {
47614771
def.name = name;
47624772
/* Java/Go: directory-based module (package), so a Go package-level var in
47634773
* myapp/db/conn.go is proj.myapp.db.Var, matching its siblings. */
4764-
def.qualified_name =
4765-
cbm_fqn_compute_source_lang(a, ctx->project, ctx->rel_path, name, ctx->language);
4774+
def.qualified_name = cbm_fqn_compute_source_lang(a, ctx->project, ctx->rel_path,
4775+
qn_name ? qn_name : name, ctx->language);
47664776
def.label = "Variable";
47674777
def.file_path = ctx->rel_path;
47684778
def.start_line = ts_node_start_point(node).row + TS_LINE_OFFSET;
@@ -4771,6 +4781,10 @@ static void push_var_def(CBMExtractCtx *ctx, const char *name, TSNode node) {
47714781
cbm_defs_push(&ctx->result->defs, a, def);
47724782
}
47734783

4784+
static void push_var_def(CBMExtractCtx *ctx, const char *name, TSNode node) {
4785+
push_var_def_qn(ctx, name, NULL, node);
4786+
}
4787+
47744788
// Helper: extract name from a declarator chain (C/C++/ObjC)
47754789
// declaration > init_declarator > declarator (may be pointer_declarator > identifier)
47764790
static const char *extract_c_declarator_name(CBMArena *a, TSNode decl, const char *source) {
@@ -5447,10 +5461,94 @@ static void extract_vars_config(CBMExtractCtx *ctx, TSNode node, CBMArena *a, co
54475461

54485462
/* ── Variable name extraction dispatcher ────────────────────────── */
54495463

5464+
/* Nix: a module-level `binding` whose value is neither a lambda nor an attribute
5465+
* set. Both of those are already represented — a lambda-valued binding is minted
5466+
* as a Function by the def walk, and an attrset-valued one is a scope
5467+
* (is_namespace_scope_kind) — so minting either again here would double-count it.
5468+
*
5469+
* The name is the attrpath's leaf and the QN carries the whole path, matching how
5470+
* the def walk names functions; `services.nginx.enable = true` is name `enable`,
5471+
* QN proj.file.services.nginx.enable. */
5472+
static void extract_vars_nix(CBMExtractCtx *ctx, TSNode node, CBMArena *a) {
5473+
if (strcmp(ts_node_type(node), "binding") != 0) {
5474+
return;
5475+
}
5476+
TSNode value = ts_node_child_by_field_name(node, TS_FIELD("expression"));
5477+
if (ts_node_is_null(value)) {
5478+
return;
5479+
}
5480+
if (strcmp(ts_node_type(value), "function_expression") == 0) {
5481+
return; /* already a Function */
5482+
}
5483+
if (cbm_nix_binding_is_attrset_scope(node)) {
5484+
return; /* a scope, not a value */
5485+
}
5486+
TSNode attrpath = ts_node_child_by_field_name(node, TS_FIELD("attrpath"));
5487+
TSNode leaf = cbm_nix_attrpath_last_attr(attrpath);
5488+
if (ts_node_is_null(leaf) || cbm_nix_attr_is_interpolated(leaf)) {
5489+
return;
5490+
}
5491+
char *name = cbm_node_text(a, leaf, ctx->source);
5492+
if (!name || !name[0]) {
5493+
return;
5494+
}
5495+
cbm_nix_strip_attr_quotes(name);
5496+
const char *scope = cbm_nix_attrpath_scope(a, attrpath, ctx->source);
5497+
const char *qn_name = scope ? cbm_arena_sprintf(a, "%s.%s", scope, name) : name;
5498+
push_var_def_qn(ctx, name, qn_name, node);
5499+
}
5500+
5501+
/* Mint every direct binding of one Nix binding container. */
5502+
static void extract_nix_binding_set(CBMExtractCtx *ctx, TSNode set, const CBMLangSpec *spec) {
5503+
if (ts_node_is_null(set)) {
5504+
return;
5505+
}
5506+
uint32_t n = ts_node_named_child_count(set);
5507+
for (uint32_t i = 0; i < n; i++) {
5508+
TSNode child = ts_node_named_child(set, i);
5509+
if (strcmp(ts_node_type(child), "binding") == 0) {
5510+
extract_var_names(ctx, child, spec);
5511+
}
5512+
}
5513+
}
5514+
5515+
/* Walk past a Nix file's header lambda(s) to the container(s) holding its
5516+
* file-scope bindings, minting each. Handles the curried header (`final: prev:`)
5517+
* and both containers of a `let … in { … }` file: the let's own bindings are file
5518+
* scope in the same sense a C++ file-static is, and the returned attrset's are the
5519+
* exported surface. Anything deeper is nested and deliberately skipped. */
5520+
static void extract_nix_module_vars(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec) {
5521+
TSNode cur = ts_node_named_child_count(root) > 0 ? ts_node_named_child(root, 0) : root;
5522+
/* Descend header lambdas: `{ pkgs, ... }: <body>`, `final: prev: <body>`. */
5523+
for (int hop = 0; hop < NIX_HEADER_HOP_MAX && !ts_node_is_null(cur) &&
5524+
strcmp(ts_node_type(cur), "function_expression") == 0;
5525+
hop++) {
5526+
cur = ts_node_child_by_field_name(cur, TS_FIELD("body"));
5527+
}
5528+
if (ts_node_is_null(cur)) {
5529+
return;
5530+
}
5531+
if (strcmp(ts_node_type(cur), "let_expression") == 0) {
5532+
extract_nix_binding_set(ctx, cbm_find_child_by_kind(cur, "binding_set"), spec);
5533+
cur = ts_node_child_by_field_name(cur, TS_FIELD("body"));
5534+
if (ts_node_is_null(cur)) {
5535+
return;
5536+
}
5537+
}
5538+
const char *k = ts_node_type(cur);
5539+
if (strcmp(k, "attrset_expression") == 0 || strcmp(k, "rec_attrset_expression") == 0) {
5540+
extract_nix_binding_set(ctx, cbm_find_child_by_kind(cur, "binding_set"), spec);
5541+
}
5542+
}
5543+
54505544
static void extract_var_names(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec) {
54515545
(void)spec;
54525546
CBMArena *a = ctx->arena;
54535547
const char *kind = ts_node_type(node);
5548+
if (ctx->language == CBM_LANG_NIX) {
5549+
extract_vars_nix(ctx, node, a);
5550+
return;
5551+
}
54545552

54555553
switch (ctx->language) {
54565554
/* Mainstream + C-family + Rust */
@@ -5684,6 +5782,22 @@ static void extract_variables(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec
56845782
return;
56855783
}
56865784

5785+
/* Nix: the file's top level sits behind its header lambda(s), so the root's
5786+
* only child is a function_expression and the generic loop below would see
5787+
* nothing. Resolve past the header to the binding container(s) that actually
5788+
* constitute file scope, and mint only THEIR direct bindings.
5789+
*
5790+
* That bound is the point. Every Nix binding's parent is a binding_set at any
5791+
* depth, so admitting them all would mint a node per `enable = true` in a
5792+
* NixOS module's settings tree — the per-leaf flood the Helm values.yaml case
5793+
* above exists to avoid. C++ mints file-scope declarations and never locals;
5794+
* this is the same rule applied to a language whose file scope is behind a
5795+
* lambda. */
5796+
if (ctx->language == CBM_LANG_NIX) {
5797+
extract_nix_module_vars(ctx, root, spec);
5798+
return;
5799+
}
5800+
56875801
// `root` is the file's root node (the sole caller passes ctx->root), so it
56885802
// is the parent of every top-level child and the module-level check is
56895803
// invariant across the loop — hoist it out (and it's now O(1) via _p).

0 commit comments

Comments
 (0)