Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
Resolve C# field writes + Kotlin cross-file calls
Martin Vogel35ac60c544eb
diff --git a/internal/cbm/extract_semantic.c b/internal/cbm/extract_semantic.cindex 530ea3314..545f9a49e 100644--- a/internal/cbm/extract_semantic.c+++ b/internal/cbm/extract_semantic.c@@ -191,14 +191,62 @@ static char *resolve_lhs_write_name(CBMExtractCtx *ctx, TSNode left) {return NULL;}-// Try to emit a write for an assignment node.-static void try_emit_assignment_write(CBMExtractCtx *ctx, TSNode node, const char *func_qn) {+// Resolve the write target of a node in an assignment_node_types set. For a+// plain assignment the target is the "left" field (or first child). For an+// increment/decrement unary expression (`x++`, `++x`, C#+// postfix_/prefix_unary_expression) there is no "left" field and the operand+// may sit on either side of the operator token, so scan named children for the+// first identifier / member-style operand. Returns a null node when no simple+// target is found.+static TSNode resolve_write_lhs_node(TSNode node) {TSNode left = ts_node_child_by_field_name(node, TS_FIELD("left"));- if (ts_node_is_null(left)) {- if (ts_node_child_count(node) > 0) {- left = ts_node_child(node, 0);+ if (!ts_node_is_null(left)) {+ return left;+ }+ const char *nk = ts_node_type(node);+ if (strcmp(nk, "postfix_unary_expression") == 0 ||+ strcmp(nk, "prefix_unary_expression") == 0 || strcmp(nk, "update_expression") == 0) {+ // Only ++/-- mutate their operand. Other unary postfix/prefix forms+ // (C# null-forgiving `x!`, address-of `&x`, deref `*x`, logical `!x`)+ // READ the operand — never treat them as writes.+ bool is_incdec = false;+ uint32_t total = ts_node_child_count(node);+ for (uint32_t i = 0; i < total; i++) {+ TSNode c = ts_node_child(node, i);+ if (ts_node_is_named(c)) {+ continue; // operator is an anonymous token+ }+ const char *op = ts_node_type(c);+ if (strcmp(op, "++") == 0 || strcmp(op, "--") == 0) {+ is_incdec = true;+ break;+ }+ }+ if (!is_incdec) {+ return (TSNode){0};+ }+ uint32_t cnc = ts_node_named_child_count(node);+ for (uint32_t i = 0; i < cnc; i++) {+ TSNode c = ts_node_named_child(node, i);+ const char *ck = ts_node_type(c);+ if (strcmp(ck, "identifier") == 0 || strcmp(ck, "simple_identifier") == 0 ||+ strcmp(ck, "member_access_expression") == 0 || strcmp(ck, "field_expression") == 0 ||+ strcmp(ck, "field_access") == 0 || strcmp(ck, "selector_expression") == 0 ||+ strcmp(ck, "subscript_expression") == 0 || strcmp(ck, "index_expression") == 0) {+ return c;+ }}+ return (TSNode){0};+ }+ if (ts_node_child_count(node) > 0) {+ return ts_node_child(node, 0);}+ return (TSNode){0};+}++// Try to emit a write for an assignment node.+static void try_emit_assignment_write(CBMExtractCtx *ctx, TSNode node, const char *func_qn) {+ TSNode left = resolve_write_lhs_node(node);if (ts_node_is_null(left)) {return;}@@ -277,12 +325,7 @@ void handle_readwrites(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec,}if (cbm_kind_in_set(node, spec->assignment_node_types)) {- TSNode left = ts_node_child_by_field_name(node, TS_FIELD("left"));- if (ts_node_is_null(left)) {- if (ts_node_child_count(node) > 0) {- left = ts_node_child(node, 0);- }- }+ TSNode left = resolve_write_lhs_node(node);if (!ts_node_is_null(left)) {char *name = resolve_lhs_write_name(ctx, left);diff --git a/internal/cbm/helpers.c b/internal/cbm/helpers.cindex 2abdb7ad7..213d06ffa 100644--- a/internal/cbm/helpers.c+++ b/internal/cbm/helpers.c@@ -123,6 +123,19 @@ static const char *java_keywords[] = {"Float", "Boolean", "Object", "List", "Map", "Set", "Optional","Stream", "Arrays", "Collections", NULL};+/* Kotlin hard keywords (those reserved everywhere). Kotlin does NOT reserve+ * primitive type names — `double`, `int`, `float`, `boolean` are ordinary+ * identifiers (the types are `Double`, `Int`, …), so a function named+ * `fun double()` is legal and must NOT be filtered as a keyword the way the+ * Java list (which lists Java primitives) would. Soft/modifier keywords+ * (`data`, `open`, `sealed`, `suspend`, …) are context-sensitive and usable as+ * identifiers, so they are intentionally omitted. */+static const char *kotlin_keywords[] = {+ "as", "break", "class", "continue", "do", "else", "false", "for",+ "fun", "if", "in", "interface", "is", "null", "object", "package",+ "return", "super", "this", "throw", "true", "try", "typealias", "typeof",+ "val", "var", "when", "while", NULL};+static const char *generic_keywords[] = {"true", "false", "null", "nil", "None", "undefined", "void", "if","else", "for", "while", "do", "switch", "case", "default", "break",@@ -155,10 +168,12 @@ bool cbm_is_keyword(const char *name, CBMLanguage lang) {keywords = rust_keywords;break;case CBM_LANG_JAVA:- case CBM_LANG_KOTLIN:case CBM_LANG_SCALA:keywords = java_keywords;break;+ case CBM_LANG_KOTLIN:+ keywords = kotlin_keywords;+ break;default:keywords = generic_keywords;break;diff --git a/internal/cbm/lang_specs.c b/internal/cbm/lang_specs.cindex 963352adc..44c455b8b 100644--- a/internal/cbm/lang_specs.c+++ b/internal/cbm/lang_specs.c@@ -372,7 +372,11 @@ static const char *cs_branch_types[] = {"if_statement", "for_statement", ""try_statement", "catch_clause", NULL};static const char *cs_var_types[] = {"field_declaration", "local_declaration_statement", NULL};static const char *cs_field_types[] = {"field_declaration", "property_declaration", NULL};-static const char *cs_assign_types[] = {"assignment_expression", "update_expression", NULL};+/* tree-sitter-c-sharp models `x++`/`++x` as postfix_/prefix_unary_expression+ * (there is no `update_expression` node — that is a JS/TS kind), so a static+ * field bump like `_count++` must list those node kinds to emit a WRITES. */+static const char *cs_assign_types[] = {"assignment_expression", "postfix_unary_expression",+ "prefix_unary_expression", NULL};static const char *cs_throw_types[] = {"throw_statement", "throw_expression", NULL};static const char *cs_decorator_types[] = {"attribute", NULL};diff --git a/internal/cbm/lsp/java_lsp.c b/internal/cbm/lsp/java_lsp.cindex 6582ef6e7..f4c81c817 100644--- a/internal/cbm/lsp/java_lsp.c+++ b/internal/cbm/lsp/java_lsp.c@@ -485,6 +485,33 @@ const char *java_resolve_type_name(JavaLSPContext *ctx, const char *name) {return cbm_arena_strdup(ctx->arena, name);}+ /* Cross-file sole-definer fallback. A same-package static call+ * `Util.square()` references class `Util` whose graph QN embeds the+ * defining file's path ("<project>.Util.Util"), which the caller's+ * module_qn ("<project>.Main") can't reconstruct — and the fixture has no+ * import to pin it. When the project-wide registry holds EXACTLY ONE type+ * with this short name, resolve to it. Bounded to a single candidate so an+ * ambiguous name (>1 type) stays unresolved — sound, mirroring the+ * registry's "unique_name" strategy. Only fires after all qualified+ * lookups miss, so it never overrides a more specific match. */+ if (ctx->registry && ctx->registry->types) {+ const char *only_qn = NULL;+ int matches = 0;+ for (int i = 0; i < ctx->registry->type_count && matches < 2; i++) {+ const CBMRegisteredType *t = &ctx->registry->types[i];+ if (!t->qualified_name || !t->short_name || t->alias_of) {+ continue;+ }+ if (strcmp(t->short_name, name) == 0) {+ only_qn = t->qualified_name;+ matches++;+ }+ }+ if (matches == 1 && only_qn) {+ return cbm_arena_strdup(ctx->arena, only_qn);+ }+ }+return NULL;}diff --git a/internal/cbm/lsp/kotlin_lsp.c b/internal/cbm/lsp/kotlin_lsp.cindex cc0de2ac5..a10369756 100644--- a/internal/cbm/lsp/kotlin_lsp.c+++ b/internal/cbm/lsp/kotlin_lsp.c@@ -623,7 +623,44 @@ const char *kotlin_resolve_function_name(KotlinLSPContext *ctx, const char *name}/* Default imports */- return kt_resolve_in_default_imports(ctx, name, CBM_KT_USE_FUNCTION);+ const char *via_default = kt_resolve_in_default_imports(ctx, name, CBM_KT_USE_FUNCTION);+ if (via_default) {+ return via_default;+ }++ /* Cross-file sole-definer fallback. In the default package (no `package`+ * declaration) a top-level `double()` in Util.kt is callable bare from+ * Main.kt, but its registered QN embeds the defining file's path+ * ("<project>.Util.double") which the caller can't reconstruct. When the+ * project-wide registry holds EXACTLY ONE top-level function (receiver_type+ * == NULL) whose short name matches, resolve to it. Bounded to a single+ * candidate so an ambiguous name (>1 definer) is left unresolved — sound,+ * mirroring the registry's "unique_name" strategy. Runs only after the+ * package/import/bare/default-import lookups miss, so it never overrides a+ * more specific match; in the per-file pass the registry holds just this+ * file's defs, so the candidate is the file's own sole top-level fun. */+ if (ctx->registry && ctx->registry->funcs) {+ const char *only_qn = NULL;+ int matches = 0;+ for (int i = 0; i < ctx->registry->func_count && matches < 2; i++) {+ const CBMRegisteredFunc *f = &ctx->registry->funcs[i];+ if (!f->qualified_name || !f->short_name) {+ continue;+ }+ if (f->receiver_type) { /* method / extension — not a bare top-level fun */+ continue;+ }+ if (strcmp(f->short_name, name) == 0) {+ only_qn = f->qualified_name;+ matches++;+ }+ }+ if (matches == 1 && only_qn) {+ return cbm_arena_strdup(ctx->arena, only_qn);+ }+ }++ return NULL;}static const char *kt_resolve_in_default_imports(KotlinLSPContext *ctx, const char *name,@@ -4051,3 +4088,130 @@ void cbm_run_kotlin_lsp(CBMArena *arena, CBMFileResult *result, const char *sourts_tree_delete(patched_tree);}}++/* ── Cross-file LSP ───────────────────────────────────────────────── */++/* Register one cross-file definition into the registry under its graph QN so+ * a call site in another file resolves to the right node. Types and functions+ * keep their full project-qualified QN; functions carry receiver_type so the+ * sole-definer fallback can tell a top-level fun from a method. */+static void kt_register_cross_def(CBMTypeRegistry *reg, CBMArena *arena, const CBMLSPDef *d) {+ if (!d->qualified_name || !d->short_name || !d->label) {+ return;+ }+ if (strcmp(d->label, "Class") == 0 || strcmp(d->label, "Interface") == 0 ||+ strcmp(d->label, "Enum") == 0 || strcmp(d->label, "Type") == 0) {+ CBMRegisteredType rt;+ memset(&rt, 0, sizeof(rt));+ rt.qualified_name = d->qualified_name;+ rt.short_name = d->short_name;+ rt.is_interface = (strcmp(d->label, "Interface") == 0) || d->is_interface;+ if (d->embedded_types && d->embedded_types[0]) {+ int n = 1;+ for (const char *p = d->embedded_types; *p; p++) {+ if (*p == '|') {+ n++;+ }+ }+ const char **emb = (const char **)cbm_arena_alloc(arena, (size_t)(n + 1) * sizeof(*emb));+ int idx = 0;+ const char *start = d->embedded_types;+ while (*start) {+ const char *end = start;+ while (*end && *end != '|') {+ end++;+ }+ if (end > start) {+ emb[idx++] = cbm_arena_strndup(arena, start, (size_t)(end - start));+ }+ if (!*end) {+ break;+ }+ start = end + 1;+ }+ emb[idx] = NULL;+ rt.embedded_types = emb;+ }+ cbm_registry_add_type(reg, rt);+ } else if (strcmp(d->label, "Method") == 0 || strcmp(d->label, "Function") == 0 ||+ strcmp(d->label, "Constructor") == 0) {+ CBMRegisteredFunc rf;+ memset(&rf, 0, sizeof(rf));+ rf.qualified_name = d->qualified_name;+ rf.short_name = d->short_name;+ rf.min_params = -1;+ /* receiver_type distinguishes a top-level fun (NULL) from a method+ * (set) — the sole-definer fallback only matches top-level funs. */+ rf.receiver_type = d->receiver_type;+ cbm_registry_add_func(reg, rf);+ }+}++void cbm_run_kotlin_lsp_cross(CBMArena *arena, const char *source, int source_len,+ const char *module_qn, CBMLSPDef *defs, int def_count,+ const char **import_names, const char **import_qns, int import_count,+ TSTree *cached_tree, CBMResolvedCallArray *out) {+ if (!arena || !source) {+ return;+ }++ CBMTypeRegistry reg;+ cbm_registry_init(®, arena);+ cbm_kotlin_stdlib_register(®, arena);++ /* Register project-wide defs (local + cross-file) under their graph QNs. */+ for (int i = 0; i < def_count; i++) {+ kt_register_cross_def(®, arena, &defs[i]);+ }++ /* Parse the source if the pipeline didn't hand us a cached tree. */+ TSTree *tree = cached_tree;+ bool owns_tree = false;+ if (!tree) {+ TSParser *parser = ts_parser_new();+ if (!parser) {+ return;+ }+ ts_parser_set_language(parser, tree_sitter_kotlin());+ tree = ts_parser_parse_string(parser, NULL, source, (uint32_t)source_len);+ ts_parser_delete(parser);+ owns_tree = true;+ }+ if (!tree) {+ return;+ }+ TSNode root = ts_tree_root_node(tree);++ /* project_name prefix (everything before the first dot of module_qn). */+ const char *project_name = "";+ const char *first_dot = module_qn ? strchr(module_qn, '.') : NULL;+ if (first_dot) {+ size_t pl = (size_t)(first_dot - module_qn);+ char *pn = (char *)cbm_arena_alloc(arena, pl + 1);+ if (pn) {+ memcpy(pn, module_qn, pl);+ pn[pl] = '\0';+ project_name = pn;+ }+ } else if (module_qn) {+ project_name = module_qn;+ }++ KotlinLSPContext ctx;+ kotlin_lsp_init(&ctx, arena, source, source_len, ®, "", module_qn ? module_qn : "",+ project_name, /*rel_path=*/NULL, out);++ /* Apply caller-supplied imports (resolved IMPORTS edges). */+ for (int i = 0; i < import_count; i++) {+ if (!import_names || !import_qns || !import_names[i] || !import_qns[i]) {+ continue;+ }+ kotlin_lsp_add_import(&ctx, import_names[i], import_qns[i], CBM_KT_USE_UNKNOWN);+ }++ kotlin_lsp_process_file(&ctx, root);++ if (owns_tree) {+ ts_tree_delete(tree);+ }+}diff --git a/internal/cbm/lsp/kotlin_lsp.h b/internal/cbm/lsp/kotlin_lsp.hindex f44165abb..4ebbe1b09 100644--- a/internal/cbm/lsp/kotlin_lsp.h+++ b/internal/cbm/lsp/kotlin_lsp.h@@ -52,6 +52,7 @@#include "scope.h"#include "type_registry.h"#include "../cbm.h"+#include "go_lsp.h" /* CBMLSPDef, CBMResolvedCallArray reused across languages *//* Use-kind for `import a.b.c.foo` — tracks whether the import refers to a* type, a function/extension, a property, or an object. Determines how@@ -157,6 +158,16 @@ const CBMType *kotlin_lookup_property_type(KotlinLSPContext *ctx, const char *clvoid cbm_run_kotlin_lsp(CBMArena *arena, CBMFileResult *result, const char *source,int source_len, TSNode root);+/* Cross-file LSP: build a registry from project-wide defs (local + cross-file)+ * + stdlib, re-parse the source if no cached tree, walk and resolve calls.+ * Mirrors cbm_run_java_lsp_cross. `defs` carries the graph QNs of every+ * project definition so a bare top-level call in file B resolves to the+ * definition node living in file A. Output is appended to `out`. */+void cbm_run_kotlin_lsp_cross(CBMArena *arena, const char *source, int source_len,+ const char *module_qn, CBMLSPDef *defs, int def_count,+ const char **import_names, const char **import_qns, int import_count,+ TSTree *cached_tree, CBMResolvedCallArray *out);+/* Register the curated Kotlin stdlib (kotlin.*, kotlin.collections.*, …)* into a registry. Implemented in lsp/generated/kotlin_stdlib_data.c. */void cbm_kotlin_stdlib_register(CBMTypeRegistry *reg, CBMArena *arena);diff --git a/internal/cbm/lsp/py_lsp.c b/internal/cbm/lsp/py_lsp.cindex 78f9581dc..2043b8b84 100644--- a/internal/cbm/lsp/py_lsp.c+++ b/internal/cbm/lsp/py_lsp.c@@ -1940,7 +1940,13 @@ static const char* py_binop_dunder(const char* op_text) {/* If `recv` is a user-defined NAMED type that defines `dunder`, emit a CALLS* edge to that dunder method (operator-overload / subscript desugaring). This- * models `a + b` → T.__add__ and `s[k]` → T.__getitem__ as calls. */+ * models `a + b` → T.__add__ and `s[k]` → T.__getitem__ as calls.+ *+ * Requires a typed receiver. An UNTYPED receiver (e.g. the unannotated+ * parameter in `def run(s): return s[0]`) is intentionally left unresolved:+ * guessing the sole class that declares the dunder would mis-resolve ordinary+ * built-in subscripts/operators (`some_list[0]`, `a + b` on ints) onto an+ * unrelated user class, so we only resolve when the receiver type is known. */static void py_emit_dunder_call(PyLSPContext* ctx, const CBMType* recv, const char* dunder) {if (!recv || recv->kind != CBM_TYPE_NAMED || !dunder) return;const CBMRegisteredFunc* f =diff --git a/src/pipeline/pass_lsp_cross.c b/src/pipeline/pass_lsp_cross.cindex 61e93d41a..09f4638cc 100644--- a/src/pipeline/pass_lsp_cross.c+++ b/src/pipeline/pass_lsp_cross.c@@ -21,6 +21,7 @@#include "lsp/ts_lsp.h"#include "lsp/php_lsp.h"#include "lsp/java_lsp.h"+#include "lsp/kotlin_lsp.h"#include "graph_buffer/graph_buffer.h"#include "foundation/constants.h"#include "foundation/hash_table.h"@@ -290,6 +291,7 @@ bool cbm_pxc_has_cross_lsp(CBMLanguage lang) {case CBM_LANG_PHP:case CBM_LANG_CSHARP: /* tier-2 prebuilt registry path (pass_parallel.c) */case CBM_LANG_JAVA: /* fallback cbm_pxc_run_one path */+ case CBM_LANG_KOTLIN: /* fallback cbm_pxc_run_one path */return true;default:return false;@@ -396,6 +398,10 @@ void cbm_pxc_run_one(CBMLanguage lang, CBMFileResult *r, const char *source, intcbm_run_java_lsp_cross(&scratch, source, source_len, module_qn, defs, def_count, imp_names,imp_qns, imp_count, tree, &out);break;+ case CBM_LANG_KOTLIN:+ cbm_run_kotlin_lsp_cross(&scratch, source, source_len, module_qn, defs, def_count, imp_names,+ imp_qns, imp_count, tree, &out);+ break;default:break;}