@@ -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+
8491017static 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 ;
0 commit comments