Skip to content

Commit 8f06867

Browse files
authored
Merge pull request DeusData#1305 from sini/feat/nix-attrpath-and-variables
fix(extract): model Nix attrpaths (leaf name, scoped QN) and mint Variables for module-level bindings
2 parents 08f6d1d + 4397833 commit 8f06867

7 files changed

Lines changed: 636 additions & 22 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 178 additions & 15 deletions
Large diffs are not rendered by default.

‎internal/cbm/extract_unified.c‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ static const char *compute_func_qn(CBMExtractCtx *ctx, TSNode node, const CBMLan
623623
return NULL;
624624
}
625625

626-
char *name = cbm_func_name_node_text(ctx->arena, name_node, ctx->source);
626+
char *name = cbm_func_name_node_text(ctx->arena, name_node, ctx->source, ctx->language);
627627
if (!name || !name[0]) {
628628
return NULL;
629629
}
@@ -644,12 +644,24 @@ static const char *compute_func_qn(CBMExtractCtx *ctx, TSNode node, const CBMLan
644644
}
645645
}
646646

647+
/* Nix: a binding's own attrpath contributes scope (`a.b.fn = …`), and the def
648+
* extractor bakes it into the def QN. Compose it identically here — otherwise
649+
* an in-body call sources to a QN one or more segments short of the def, and
650+
* the edge is dropped at write. */
651+
const char *qn_name = name;
652+
if (ctx->language == CBM_LANG_NIX) {
653+
qn_name = cbm_nix_qn_name(ctx->arena, node, ctx->source, name);
654+
if (!qn_name || !qn_name[0]) {
655+
return NULL;
656+
}
657+
}
658+
647659
if (state->enclosing_class_qn) {
648-
return cbm_arena_sprintf(ctx->arena, "%s.%s", state->enclosing_class_qn, name);
660+
return cbm_arena_sprintf(ctx->arena, "%s.%s", state->enclosing_class_qn, qn_name);
649661
}
650662
/* Java/Go: directory-based module so this enclosing-func QN matches the def
651663
* QN and the LSP caller_qn (the lsp_resolve join keys on exact equality). */
652-
return cbm_fqn_compute_source_lang(ctx->arena, ctx->project, ctx->rel_path, name,
664+
return cbm_fqn_compute_source_lang(ctx->arena, ctx->project, ctx->rel_path, qn_name,
653665
ctx->language);
654666
}
655667

@@ -658,6 +670,13 @@ static const char *compute_class_qn(CBMExtractCtx *ctx, TSNode node, const WalkS
658670
if (ctx->language == CBM_LANG_OBJECTSCRIPT_UDL) {
659671
return objectscript_get_class_name(ctx, node);
660672
}
673+
/* Nix: an attrset-valued `binding` is a named scope. Same shared helper the def
674+
* extractor's compute_class_qn uses — these are two separate functions, and a
675+
* one-segment disagreement between them drops every CALLS edge sourced from a
676+
* nested binding. */
677+
if (ctx->language == CBM_LANG_NIX) {
678+
return cbm_nix_binding_scope_qn(ctx, node, state ? state->enclosing_class_qn : NULL);
679+
}
661680
TSNode name_node = ts_node_child_by_field_name(node, TS_FIELD("name"));
662681
/* Newer tree-sitter-kotlin: class/object name is a type_identifier child. */
663682
if (ts_node_is_null(name_node) && ctx->language == CBM_LANG_KOTLIN) {
@@ -1447,6 +1466,16 @@ static void push_boundary_scopes(CBMExtractCtx *ctx, TSNode node, const CBMLangS
14471466
state->os_type_map.class_base_count = 0;
14481467
}
14491468
}
1469+
} else if (ctx->language == CBM_LANG_NIX && strcmp(ts_node_type(node), "binding") == 0 &&
1470+
cbm_nix_binding_is_attrset_scope(node)) {
1471+
/* Nix: a binding whose value is an attribute set is a named scope, exactly
1472+
* as is_namespace_scope_kind treats it on the def side. Pushing it here is
1473+
* what makes an in-body call source to `proj.file.setA.fn` rather than the
1474+
* bare `proj.file.fn` that no node carries once defs are attrpath-qualified. */
1475+
const char *cqn = compute_class_qn(ctx, node, state);
1476+
if (cqn) {
1477+
push_scope(state, SCOPE_CLASS, depth, cqn);
1478+
}
14501479
} else if (ctx->language == CBM_LANG_RUST && strcmp(ts_node_type(node), "impl_item") == 0) {
14511480
TSNode type_node = ts_node_child_by_field_name(node, TS_FIELD("type"));
14521481
if (!ts_node_is_null(type_node)) {

‎internal/cbm/helpers.c‎

Lines changed: 170 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ TSNode cbm_resolve_c_declarator_name_node(TSNode func_node) {
832832
// space. Without this the conversion operator is indexed as "operator bool()
833833
// const", and a member lookup for "operator bool" (the implicit call in
834834
// `if (obj)`) misses.
835-
char *cbm_func_name_node_text(CBMArena *a, TSNode name_node, const char *source) {
835+
char *cbm_func_name_node_text(CBMArena *a, TSNode name_node, const char *source, CBMLanguage lang) {
836836
char *text = cbm_node_text(a, name_node, source);
837837
if (text && strcmp(ts_node_type(name_node), "operator_cast") == 0) {
838838
char *paren = strchr(text, '(');
@@ -843,9 +843,177 @@ char *cbm_func_name_node_text(CBMArena *a, TSNode name_node, const char *source)
843843
*paren = '\0';
844844
}
845845
}
846+
/* Nix quoted attrpath segment: `"kebab-case" = a: a;` and `services."my.svc" = …`
847+
* are ordinary names that merely need quoting in source. The node text carries
848+
* the delimiters, so without this the def is named `"kebab-case"` — quotes and
849+
* all — and every consumer keying on the name (search, CALLS resolution, the
850+
* LSP join) would have to know to re-quote. Stripped here rather than in the
851+
* resolver so the def name and the call-scope QN, which both route through this
852+
* function, cannot disagree. */
853+
if (text && lang == CBM_LANG_NIX) {
854+
cbm_nix_strip_attr_quotes(text);
855+
}
846856
return text;
847857
}
848858

859+
/* ── Nix attrpath helpers ───────────────────────────────────
860+
* A Nix binding's name is a PATH (`a.b.c = …`), whose segments may be quoted or
861+
* interpolated. These render it the way the rest of the extractor expects: leaf
862+
* segment as the name, leading segments as scope. Shared by the defs and unified
863+
* (call-scope) extractors so both compute the same QN — if they disagree, a CALLS
864+
* edge names a source node that does not exist and is dropped at write, which is
865+
* precisely how the function-header bug manifested. */
866+
867+
/* Bound on the interpolation scan below. An attrpath segment is an identifier or a
868+
* string, so its subtree is shallow; this only has to stop a pathological input. */
869+
enum { NIX_ATTR_SCAN_MAX = 32 };
870+
871+
/* Strip one matching pair of surrounding double quotes, in place. */
872+
void cbm_nix_strip_attr_quotes(char *text) {
873+
if (!text) {
874+
return;
875+
}
876+
size_t len = strlen(text);
877+
if (len >= CBM_QUOTE_PAIR && text[0] == '"' && text[len - SKIP_ONE] == '"') {
878+
memmove(text, text + SKIP_ONE, len - PAIR_LEN);
879+
text[len - PAIR_LEN] = '\0';
880+
}
881+
}
882+
883+
/* True when a segment contains a `${...}` interpolation, and therefore has no
884+
* statically knowable name. Iterative and bounded — the lint forbids unlisted
885+
* recursion, and an attrpath segment is shallow by construction. */
886+
bool cbm_nix_attr_is_interpolated(TSNode attr) {
887+
if (ts_node_is_null(attr)) {
888+
return false;
889+
}
890+
TSNode stack[NIX_ATTR_SCAN_MAX];
891+
int top = 0;
892+
stack[top++] = attr;
893+
while (top > 0) {
894+
TSNode cur = stack[--top];
895+
if (strcmp(ts_node_type(cur), "interpolation") == 0) {
896+
return true;
897+
}
898+
uint32_t n = ts_node_named_child_count(cur);
899+
for (uint32_t i = 0; i < n && top < NIX_ATTR_SCAN_MAX; i++) {
900+
stack[top++] = ts_node_named_child(cur, i);
901+
}
902+
}
903+
return false;
904+
}
905+
906+
/* The leaf segment of an attrpath — the name. `attr` is a FIELD in this grammar,
907+
* not a node type (segments are identifier / string_expression / interpolation),
908+
* so iterate named children rather than matching on a type string. */
909+
TSNode cbm_nix_attrpath_last_attr(TSNode attrpath) {
910+
TSNode last = {0};
911+
if (ts_node_is_null(attrpath)) {
912+
return last;
913+
}
914+
uint32_t n = ts_node_named_child_count(attrpath);
915+
if (n == 0) {
916+
return last;
917+
}
918+
return ts_node_named_child(attrpath, n - SKIP_ONE);
919+
}
920+
921+
/* The scope prefix of an attrpath: every segment except the leaf, quote-stripped
922+
* and dot-joined. `a.b.fn = …` yields "a.b" so it qualifies identically to the
923+
* nested spelling `a = { b = { fn = …; }; }`. Returns NULL for a single-segment
924+
* path (no scope) or when a leading segment is interpolated (not nameable). */
925+
const char *cbm_nix_attrpath_scope(CBMArena *a, TSNode attrpath, const char *source) {
926+
if (ts_node_is_null(attrpath)) {
927+
return NULL;
928+
}
929+
uint32_t n = ts_node_named_child_count(attrpath);
930+
if (n <= SKIP_ONE) {
931+
return NULL;
932+
}
933+
const char *scope = NULL;
934+
for (uint32_t i = 0; i + SKIP_ONE < n; i++) {
935+
TSNode seg = ts_node_named_child(attrpath, i);
936+
if (cbm_nix_attr_is_interpolated(seg)) {
937+
return NULL;
938+
}
939+
char *seg_text = cbm_node_text(a, seg, source);
940+
if (!seg_text || !seg_text[0]) {
941+
return NULL;
942+
}
943+
cbm_nix_strip_attr_quotes(seg_text);
944+
scope = scope ? cbm_arena_sprintf(a, "%s.%s", scope, seg_text) : seg_text;
945+
}
946+
return scope;
947+
}
948+
949+
/* True when a Nix `binding`'s value is an attribute set, i.e. the binding names a
950+
* scope rather than defining a value. Deliberately excludes `let` bindings and
951+
* lambda-valued bindings: the former are lexical (C++ does not qualify by block
952+
* scope either), the latter are definitions in their own right. */
953+
bool cbm_nix_binding_is_attrset_scope(TSNode node) {
954+
if (ts_node_is_null(node) || strcmp(ts_node_type(node), "binding") != 0) {
955+
return false;
956+
}
957+
TSNode value = ts_node_child_by_field_name(node, TS_FIELD("expression"));
958+
if (ts_node_is_null(value)) {
959+
return false;
960+
}
961+
const char *vk = ts_node_type(value);
962+
return strcmp(vk, "attrset_expression") == 0 || strcmp(vk, "rec_attrset_expression") == 0;
963+
}
964+
965+
/* The scope QN contributed by a Nix `binding` whose value is an attribute set —
966+
* `setA = { … }` contributes "…file.setA", and a dotted `a.b = { … }` contributes
967+
* "…file.a.b". Returns saved_enclosing unchanged when the binding cannot name a
968+
* scope (empty or interpolated attrpath).
969+
*
970+
* Lives here, called by BOTH extract_defs.c and extract_unified.c, because those
971+
* two files carry separate compute_class_qn implementations. A def QN and a
972+
* call-scope QN that disagree by even one segment make the CALLS edge name a
973+
* source node that was never minted, and it is silently dropped at write. Sharing
974+
* the computation makes that class of drift impossible rather than merely
975+
* unlikely. */
976+
const char *cbm_nix_binding_scope_qn(CBMExtractCtx *ctx, TSNode node, const char *saved_enclosing) {
977+
if (!ctx || ts_node_is_null(node) || strcmp(ts_node_type(node), "binding") != 0) {
978+
return saved_enclosing;
979+
}
980+
TSNode attrpath = ts_node_child_by_field_name(node, TS_FIELD("attrpath"));
981+
TSNode leaf = cbm_nix_attrpath_last_attr(attrpath);
982+
if (ts_node_is_null(leaf) || cbm_nix_attr_is_interpolated(leaf)) {
983+
return saved_enclosing;
984+
}
985+
char *leaf_text = cbm_node_text(ctx->arena, leaf, ctx->source);
986+
if (!leaf_text || !leaf_text[0]) {
987+
return saved_enclosing;
988+
}
989+
cbm_nix_strip_attr_quotes(leaf_text);
990+
const char *scope = cbm_nix_attrpath_scope(ctx->arena, attrpath, ctx->source);
991+
const char *rel = scope ? cbm_arena_sprintf(ctx->arena, "%s.%s", scope, leaf_text) : leaf_text;
992+
if (saved_enclosing) {
993+
return cbm_arena_sprintf(ctx->arena, "%s.%s", saved_enclosing, rel);
994+
}
995+
return cbm_fqn_compute_source_lang(ctx->arena, ctx->project, ctx->rel_path, rel, ctx->language);
996+
}
997+
998+
/* The QN-relative name of a Nix binding: its attrpath scope joined to its leaf
999+
* name. `a.b.fn = …` yields "a.b.fn"; a bare `fn = …` yields "fn". Callers prepend
1000+
* either the enclosing attrset scope or the module QN, so the two scope sources —
1001+
* a dotted attrpath and an enclosing attrset — compose. Takes the already-resolved
1002+
* leaf name rather than re-deriving it, so it cannot disagree with the name the
1003+
* def was minted under. */
1004+
const char *cbm_nix_qn_name(CBMArena *a, TSNode func_node, const char *source, const char *name) {
1005+
if (!name) {
1006+
return NULL;
1007+
}
1008+
TSNode parent = ts_node_parent(func_node);
1009+
if (ts_node_is_null(parent) || strcmp(ts_node_type(parent), "binding") != 0) {
1010+
return name;
1011+
}
1012+
TSNode attrpath = ts_node_child_by_field_name(parent, TS_FIELD("attrpath"));
1013+
const char *scope = cbm_nix_attrpath_scope(a, attrpath, source);
1014+
return scope ? cbm_arena_sprintf(a, "%s.%s", scope, name) : name;
1015+
}
1016+
8491017
static const char *func_node_name(CBMArena *a, TSNode func_node, const char *source,
8501018
CBMLanguage lang) {
8511019
// Wolfram: set_delayed_top/set_top/set_delayed/set — LHS is apply(user_symbol("f"), ...)
@@ -884,7 +1052,7 @@ static const char *func_node_name(CBMArena *a, TSNode func_node, const char *sou
8841052
if (strcmp(ts_node_type(func_node), "function_definition") == 0) {
8851053
TSNode dn = cbm_resolve_c_declarator_name_node(func_node);
8861054
if (!ts_node_is_null(dn)) {
887-
return cbm_func_name_node_text(a, dn, source);
1055+
return cbm_func_name_node_text(a, dn, source, lang);
8881056
}
8891057
}
8901058
return NULL;

‎internal/cbm/helpers.h‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,46 @@ TSNode cbm_resolve_c_declarator_name_node(TSNode func_node);
6262
// C++ conversion-operator's `operator_cast` node (which spans the full
6363
// "operator bool() const") down to "operator bool". Shared by the defs and
6464
// unified extractors so the def name and call-scope QN agree.
65-
char *cbm_func_name_node_text(CBMArena *a, TSNode name_node, const char *source);
65+
// Also strips the surrounding quotes from a Nix quoted attrpath segment, so
66+
// `"kebab-case" = a: a;` is named kebab-case rather than "kebab-case". Takes the
67+
// language for that reason; every caller must pass ctx->language.
68+
char *cbm_func_name_node_text(CBMArena *a, TSNode name_node, const char *source, CBMLanguage lang);
69+
70+
// ── Nix attrpath helpers ──
71+
// A Nix binding's name is a PATH (`a.b.c = …`) whose segments may be quoted or
72+
// interpolated. Shared by the defs and unified (call-scope) extractors so both
73+
// derive the same name and the same scope prefix — divergence makes a CALLS edge
74+
// name a source node that does not exist, and it is dropped at write.
75+
76+
// Strip one matching pair of surrounding double quotes, in place.
77+
void cbm_nix_strip_attr_quotes(char *text);
78+
79+
// True when an attrpath segment contains a `${...}` interpolation and therefore
80+
// has no statically knowable name.
81+
bool cbm_nix_attr_is_interpolated(TSNode attr);
82+
83+
// The leaf segment of an attrpath — the name. Null node for an empty attrpath.
84+
TSNode cbm_nix_attrpath_last_attr(TSNode attrpath);
85+
86+
// The scope prefix of an attrpath: all segments but the leaf, quote-stripped and
87+
// dot-joined, so `a.b.fn = …` qualifies identically to `a = { b = { fn = …; }; }`.
88+
// NULL for a single-segment path, or when a leading segment is interpolated.
89+
const char *cbm_nix_attrpath_scope(CBMArena *a, TSNode attrpath, const char *source);
90+
91+
// True when a Nix `binding`'s value is an attribute set — the binding names a
92+
// scope rather than defining a value. Excludes let-bindings and lambda values.
93+
bool cbm_nix_binding_is_attrset_scope(TSNode node);
94+
95+
// The scope QN contributed by a Nix `binding` whose value is an attribute set.
96+
// Called by BOTH extract_defs.c and extract_unified.c, which carry separate
97+
// compute_class_qn implementations — sharing this makes a def/call-scope QN
98+
// mismatch (which silently drops the CALLS edge) structurally impossible.
99+
const char *cbm_nix_binding_scope_qn(CBMExtractCtx *ctx, TSNode node, const char *saved_enclosing);
100+
101+
// The QN-relative name of a Nix binding — its attrpath scope joined to `name`.
102+
// Callers prepend the enclosing attrset scope (or the module QN), so a dotted
103+
// attrpath and an enclosing attrset compose into one qualified name.
104+
const char *cbm_nix_qn_name(CBMArena *a, TSNode func_node, const char *source, const char *name);
66105

67106
// Resolve a function/method definition node's NAME node across all ~130 grammars
68107
// (generic `name` field, arrow→declarator, C/C++ declarator chain, plus the many

0 commit comments

Comments
 (0)