Skip to content

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.

feat(kotlin): receiver-typed property references decide their occurrence

Martin Vogel37d23d361963
diff --git a/internal/cbm/cbm.h b/internal/cbm/cbm.h
index c4095d7dd..440d80fce 100644
--- a/internal/cbm/cbm.h
+++ b/internal/cbm/cbm.h
@@ -587,6 +587,10 @@ typedef struct {
CBMStringConstantMap string_constants; // module-level NAME = "value" pairs
const CBMMacroTable *macro_table; // ObjectScript $$$macro table (NULL if none)
const CBMReturnTypeTable *return_type_table; // ObjectScript method return types (NULL if none)
+ /* Set by extract_class_variables around its extract_var_names calls, so a
+ * class-body variable def records which class declares it (parent_class)
+ * without changing its module-level qualified name. NULL elsewhere. */
+ const char *var_parent_class;
} CBMExtractCtx;
// --- Public API ---
diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.c
index 105f93274..3270bacc3 100644
--- a/internal/cbm/extract_defs.c
+++ b/internal/cbm/extract_defs.c
@@ -193,7 +193,8 @@ static void extract_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
static void walk_defs(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec, int depth_unused);
static void extract_variables(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec);
static void extract_var_names(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec);
-static void extract_class_variables(CBMExtractCtx *ctx, TSNode class_node, const CBMLangSpec *spec);
+static void extract_class_variables(CBMExtractCtx *ctx, TSNode class_node, const char *class_qn,
+ const CBMLangSpec *spec);
static void extract_rust_impl(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec);
static void extract_class_methods(CBMExtractCtx *ctx, TSNode class_node, const char *class_qn,
const CBMLangSpec *spec);
@@ -4385,7 +4386,7 @@ static void extract_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
extract_class_fields(ctx, node, class_qn, spec);
// Extract class-level variables (field declarations)
- extract_class_variables(ctx, node, spec);
+ extract_class_variables(ctx, node, class_qn, spec);
// C# 12 primary-constructor parameters: declared on the class line
// (`class Foo(IBar bar, IBaz baz) : Base { ... }`) and bound to implicit
@@ -5100,6 +5101,11 @@ static void push_var_def_qn(CBMExtractCtx *ctx, const char *name, const char *qn
qn_name ? qn_name : name, ctx->language);
def.label = "Variable";
def.file_path = ctx->rel_path;
+ /* Class-body variables record their declaring class (set by
+ * extract_class_variables); the QN stays module-level as before, so this
+ * is additive metadata that lets cross-file resolution attach the
+ * property to its receiver type. */
+ def.parent_class = ctx->var_parent_class;
def.start_line = ts_node_start_point(node).row + TS_LINE_OFFSET;
def.end_line = ts_node_end_point(node).row + TS_LINE_OFFSET;
def.is_exported = cbm_is_exported(name, ctx->language);
@@ -6583,7 +6589,7 @@ static void extract_class_fields(CBMExtractCtx *ctx, TSNode class_node, const ch
}
// Extract class-level variables (field declarations inside class bodies)
-static void extract_class_variables(CBMExtractCtx *ctx, TSNode class_node,
+static void extract_class_variables(CBMExtractCtx *ctx, TSNode class_node, const char *class_qn,
const CBMLangSpec *spec) {
if (!spec->variable_node_types || !spec->variable_node_types[0]) {
return;
@@ -6594,6 +6600,10 @@ static void extract_class_variables(CBMExtractCtx *ctx, TSNode class_node,
return;
}
+ /* Record the declaring class on every variable minted from this body (see
+ * push_var_def_qn); saved/restored so module-level minting stays bare. */
+ const char *saved_parent = ctx->var_parent_class;
+ ctx->var_parent_class = class_qn;
uint32_t count = ts_node_named_child_count(body);
for (uint32_t i = 0; i < count; i++) {
TSNode child = ts_node_named_child(body, i);
@@ -6601,6 +6611,7 @@ static void extract_class_variables(CBMExtractCtx *ctx, TSNode class_node,
extract_var_names(ctx, child, spec);
}
}
+ ctx->var_parent_class = saved_parent;
}
// --- Module node + main walk ---
diff --git a/internal/cbm/extract_usages.c b/internal/cbm/extract_usages.c
index 225f1d499..e519012c0 100644
--- a/internal/cbm/extract_usages.c
+++ b/internal/cbm/extract_usages.c
@@ -1558,6 +1558,16 @@ static TSNode call_reference_candidate_site(CBMExtractCtx *ctx, TSNode node, con
if (strcmp(kind, "identifier") != 0 && strcmp(kind, "simple_identifier") != 0) {
return (TSNode){0};
}
+ /* Kotlin: a navigation member read (`obj.member`, and `value::member`,
+ * which the vendored grammar also parses as navigation) is a candidate at
+ * the member occurrence, so a receiver-typed LSP row can claim it instead
+ * of the name-only registry fallback (maintainer decision, option B). With
+ * no row at the occurrence the join finds nothing and behavior is exactly
+ * as before. */
+ if (ctx->language == CBM_LANG_KOTLIN && !ts_node_is_null(parent) &&
+ strcmp(ts_node_type(parent), "navigation_suffix") == 0) {
+ return node;
+ }
/* Python: the right-hand side of `callback = handler` is a genuine value
* occurrence of handler, and when the LSP proves its exact callable
* identity it becomes a CALL_REFERENCE like any proven argument value
diff --git a/internal/cbm/lsp/kotlin_lsp.c b/internal/cbm/lsp/kotlin_lsp.c
index 63039d053..0ca49afd5 100644
--- a/internal/cbm/lsp/kotlin_lsp.c
+++ b/internal/cbm/lsp/kotlin_lsp.c
@@ -36,6 +36,21 @@
#include "kotlin_lsp.h"
#include "../helpers.h"
+/* CBMHashTable — cross-registry field bucketing. Relative path: the LSP
+ * amalgamation compiles with -Iinternal/cbm only, not -Isrc. */
+#include "../../../src/foundation/hash_table.h"
+
+/* Growable field list for one receiver type, bucketed per cross-registry call
+ * (see cbm_kotlin_register_lsp_defs). qns carries each field's REAL def QN --
+ * kotlin class properties are minted with module-level QNs, so composing
+ * <class>.<member> would name a node that does not exist. */
+typedef struct {
+ const char **names;
+ const CBMType **types;
+ const char **qns;
+ int count;
+ int cap;
+} KtCrossFieldList;
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
@@ -1021,6 +1036,19 @@ const char *kotlin_resolve_class_name(KotlinLSPContext *ctx, const char *name) {
}
}
+ /* Cross-file short-name resolution: a project type with this UNIQUE short
+ * name wins over the blind same-package guess below, which composes
+ * <this file's module>.<name> and so can never name a type defined in
+ * another file without an import. Hash lookup, no scan; an ambiguous
+ * short name resolves nothing here (fail closed) and falls through. */
+ if (ctx->cross_type_short) {
+ const char *qn =
+ (const char *)cbm_ht_get((const CBMHashTable *)ctx->cross_type_short, name);
+ if (qn && qn[0]) {
+ return qn;
+ }
+ }
+
/* Same-package fallback — even if not in registry, useful for cross-file
* lookups via the pipeline's <project>.<qn> retry. */
if (ctx->package_qn && *ctx->package_qn) {
@@ -3191,6 +3219,43 @@ static const CBMType *kt_eval_navigation_expression_type_at(KotlinLSPContext *ct
kt_emit_delegate_access(ctx, member_text, recv_qn, node, selector);
const CBMType *pt = kotlin_lookup_property_type(ctx, recv_qn, member_text);
if (!cbm_type_is_unknown(pt)) {
+ /* A property READ with a proven receiver claims its member
+ * occurrence with an exact row (maintainer decision, option B):
+ * without it the occurrence falls to the name-only registry
+ * fallback, whose winner among same-named symbols is registration
+ * order -- readdir order -- so `holder::handler` (parsed as
+ * navigation by the vendored grammar) borrowed a same-named
+ * FUNCTION from another file on Windows. The property is not a
+ * callable target, so the join can only produce USAGE, never a
+ * fabricated CALL_REFERENCE. Value reads only: when this
+ * navigation is the callee of a call, site is the call node and
+ * the invocation machinery owns the occurrence. */
+ if (ts_node_eq(site, node)) {
+ /* Prefer the field's REAL def QN from the cross field map --
+ * kotlin class properties are minted with module-level QNs, so
+ * the composed <class>.<member> form would name a node that
+ * does not exist and the join would drop the edge. */
+ const char *prop_qn = NULL;
+ if (ctx->cross_field_map) {
+ const KtCrossFieldList *fl = (const KtCrossFieldList *)cbm_ht_get(
+ (const CBMHashTable *)ctx->cross_field_map, recv_qn);
+ for (int fi = 0; fl && fi < fl->count; fi++) {
+ if (fl->names[fi] && strcmp(fl->names[fi], member_text) == 0) {
+ prop_qn = fl->qns[fi];
+ break;
+ }
+ }
+ }
+ if (!prop_qn) {
+ prop_qn = kt_join_dot(ctx->arena, recv_qn, member_text);
+ }
+ if (ctx->debug) {
+ fprintf(stderr, "[kotlin_lsp] KTPROP recv_qn=%s prop_qn=%s\n", recv_qn,
+ prop_qn);
+ }
+ kt_emit_resolved_kind_at(ctx, prop_qn, "lsp_kt_property_ref", KT_CONF_METHOD,
+ CBM_RESOLVED_CALL_REFERENCE, selector);
+ }
return pt;
}
@@ -5176,7 +5241,8 @@ static const CBMType *kt_cross_func_sig_with_return(CBMArena *arena, const CBMLS
return cbm_type_func(arena, NULL, param_types, rets);
}
-static void kt_register_cross_def(CBMTypeRegistry *reg, CBMArena *arena, const CBMLSPDef *d) {
+static void kt_register_cross_def(CBMTypeRegistry *reg, CBMArena *arena, const CBMLSPDef *d,
+ const CBMHashTable *field_map) {
if (!d->qualified_name || !d->short_name || !d->label) {
return;
}
@@ -5187,6 +5253,15 @@ static void kt_register_cross_def(CBMTypeRegistry *reg, CBMArena *arena, const C
rt.qualified_name = d->qualified_name;
rt.short_name = d->short_name;
rt.is_interface = (strcmp(d->label, "Interface") == 0) || d->is_interface;
+ if (field_map) {
+ const KtCrossFieldList *fl =
+ (const KtCrossFieldList *)cbm_ht_get((CBMHashTable *)field_map,
+ d->qualified_name);
+ if (fl && fl->count > 0) {
+ rt.field_names = fl->names;
+ rt.field_types = fl->types;
+ }
+ }
if (d->embedded_types && d->embedded_types[0]) {
int n = 1;
for (const char *p = d->embedded_types; *p; p++) {
@@ -5231,9 +5306,75 @@ static void kt_register_cross_def(CBMTypeRegistry *reg, CBMArena *arena, const C
}
void cbm_kotlin_register_lsp_defs(CBMArena *arena, CBMTypeRegistry *reg, const CBMLSPDef *defs,
- int def_count) {
+ int def_count, CBMHashTable **out_field_map) {
+ /* Pass 1: bucket property (Variable) defs by receiver type, so a class
+ * registers WITH its fields. Cross registration previously dropped
+ * properties entirely (pxc_map_label filtered the label), which left
+ * kotlin_lookup_property_type blind across files -- so `holder::handler`
+ * could not be claimed by the receiver-typed resolution and fell to the
+ * name-only fallback (maintainer decision, option B). Hash-bucketed: the
+ * per-type field scan over all defs would be the registry-tail-scan
+ * quadratic pattern. */
+ CBMHashTable *field_map = cbm_ht_create(64);
+ if (field_map) {
+ for (int i = 0; i < def_count; i++) {
+ const CBMLSPDef *d = &defs[i];
+ if (!d->qualified_name || !d->short_name || !d->label ||
+ strcmp(d->label, "Variable") != 0 || !d->receiver_type || !d->receiver_type[0]) {
+ continue;
+ }
+ KtCrossFieldList *fl = (KtCrossFieldList *)cbm_ht_get(field_map, d->receiver_type);
+ if (!fl) {
+ fl = (KtCrossFieldList *)cbm_arena_alloc(arena, sizeof(*fl));
+ if (!fl) {
+ continue;
+ }
+ memset(fl, 0, sizeof(*fl));
+ cbm_ht_set(field_map, d->receiver_type, fl);
+ }
+ if (fl->count + 1 >= fl->cap) {
+ int new_cap = fl->cap ? fl->cap * 2 : 4;
+ const char **nn =
+ (const char **)cbm_arena_alloc(arena, (size_t)new_cap * sizeof(*nn));
+ const CBMType **nt =
+ (const CBMType **)cbm_arena_alloc(arena, (size_t)new_cap * sizeof(*nt));
+ const char **nq =
+ (const char **)cbm_arena_alloc(arena, (size_t)new_cap * sizeof(*nq));
+ if (!nn || !nt || !nq) {
+ continue;
+ }
+ if (fl->count > 0) {
+ memcpy(nn, fl->names, (size_t)fl->count * sizeof(*nn));
+ memcpy(nt, fl->types, (size_t)fl->count * sizeof(*nt));
+ memcpy(nq, fl->qns, (size_t)fl->count * sizeof(*nq));
+ }
+ fl->names = nn;
+ fl->types = nt;
+ fl->qns = nq;
+ fl->cap = new_cap;
+ }
+ const CBMType *ft = kt_cross_return_type(arena, d);
+ fl->names[fl->count] = d->short_name;
+ /* The declared type when the def carries one; a generic named type
+ * otherwise, so the field's EXISTENCE is still visible to
+ * kotlin_lookup_property_type's presence gate. */
+ fl->types[fl->count] = ft ? ft : cbm_type_named(arena, "kotlin.Any");
+ fl->qns[fl->count] = d->qualified_name;
+ fl->count++;
+ fl->names[fl->count] = NULL;
+ fl->types[fl->count] = NULL;
+ fl->qns[fl->count] = NULL;
+ }
+ }
for (int i = 0; i < def_count; i++) {
- kt_register_cross_def(reg, arena, &defs[i]);
+ kt_register_cross_def(reg, arena, &defs[i], field_map);
+ }
+ if (out_field_map) {
+ /* Ownership transfers: the caller keeps the map alive for the resolve
+ * walk (property references need each field's real def QN). */
+ *out_field_map = field_map;
+ } else {
+ cbm_ht_free(field_map);
}
}
@@ -5249,8 +5390,11 @@ void cbm_run_kotlin_lsp_cross(CBMArena *arena, const char *source, int source_le
cbm_registry_init(&reg, arena);
cbm_kotlin_stdlib_register(&reg, arena);
- /* Register project-wide defs (local + cross-file) under their graph QNs. */
- cbm_kotlin_register_lsp_defs(arena, &reg, defs, def_count);
+ /* Register project-wide defs (local + cross-file) under their graph QNs.
+ * Keep the field map alive for the resolve walk: property references emit
+ * against each field's REAL def QN. */
+ CBMHashTable *cross_field_map = NULL;
+ cbm_kotlin_register_lsp_defs(arena, &reg, defs, def_count, &cross_field_map);
/* Build the hash indexes: without this every registry lookup in the walk
* is a LINEAR scan over the whole cross registry — O(lookups x defs) per
@@ -5267,6 +5411,7 @@ void cbm_run_kotlin_lsp_cross(CBMArena *arena, const char *source, int source_le
TSParser *parser = ts_parser_new();
if (!parser) {
cbm_arena_destroy(&idx_arena);
+ cbm_ht_free(cross_field_map);
return;
}
ts_parser_set_language(parser, tree_sitter_kotlin());
@@ -5276,6 +5421,7 @@ void cbm_run_kotlin_lsp_cross(CBMArena *arena, const char *source, int source_le
}
if (!tree) {
cbm_arena_destroy(&idx_arena);
+ cbm_ht_free(cross_field_map);
return;
}
TSNode root = ts_tree_root_node(tree);
@@ -5302,6 +5448,33 @@ void cbm_run_kotlin_lsp_cross(CBMArena *arena, const char *source, int source_le
kotlin_lsp_init(&ctx, arena, source, source_len, &reg, module_qn ? module_qn : "",
module_qn ? module_qn : "", project_name, /*rel_path=*/NULL, out);
+ /* Unique short type name -> registered QN, for receiver annotations that
+ * name a type defined in another file without an import. Two types
+ * sharing a short name map to "" so the lookup resolves nothing (fail
+ * closed) rather than picking one arbitrarily. Built after the early
+ * parse-failure returns so nothing leaks on those paths. */
+ CBMHashTable *type_short = cbm_ht_create(64);
+ if (type_short) {
+ for (int i = 0; i < def_count; i++) {
+ const CBMLSPDef *d = &defs[i];
+ if (!d->qualified_name || !d->short_name || !d->label) {
+ continue;
+ }
+ if (strcmp(d->label, "Class") != 0 && strcmp(d->label, "Interface") != 0 &&
+ strcmp(d->label, "Enum") != 0 && strcmp(d->label, "Type") != 0) {
+ continue;
+ }
+ const char *prev = (const char *)cbm_ht_get(type_short, d->short_name);
+ if (!prev) {
+ cbm_ht_set(type_short, d->short_name, (void *)d->qualified_name);
+ } else if (strcmp(prev, d->qualified_name) != 0) {
+ cbm_ht_set(type_short, d->short_name, (void *)"");
+ }
+ }
+ }
+ ctx.cross_type_short = type_short;
+ ctx.cross_field_map = cross_field_map;
+
/* 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]) {
@@ -5312,6 +5485,8 @@ void cbm_run_kotlin_lsp_cross(CBMArena *arena, const char *source, int source_le
kotlin_lsp_process_file(&ctx, root);
cbm_arena_destroy(&idx_arena);
+ cbm_ht_free(type_short);
+ cbm_ht_free(cross_field_map);
if (owns_tree) {
ts_tree_delete(tree);
diff --git a/internal/cbm/lsp/kotlin_lsp.h b/internal/cbm/lsp/kotlin_lsp.h
index 765088bee..98e048e6f 100644
--- a/internal/cbm/lsp/kotlin_lsp.h
+++ b/internal/cbm/lsp/kotlin_lsp.h
@@ -133,6 +133,20 @@ typedef struct KotlinLSPContext {
/* Debug mode (CBM_LSP_DEBUG env). */
bool debug;
+ /* Cross-file only (NULL per-file): unique short type name -> registered
+ * QN, built from the project defs by cbm_run_kotlin_lsp_cross. Consulted
+ * by kotlin_resolve_class_name before its blind same-package guess, so an
+ * unimported cross-file receiver type ("Holder" defined in Holder.kt,
+ * used in Use.kt) resolves to its real registered QN. An ambiguous short
+ * name maps to a sentinel and resolves nothing (fail closed). Owned by
+ * cbm_run_kotlin_lsp_cross; opaque pointer to avoid a foundation include
+ * in this header. */
+ const void *cross_type_short;
+ /* Cross-file only (NULL per-file): receiver type QN -> KtCrossFieldList,
+ * from cbm_kotlin_register_lsp_defs. Property-reference emission reads
+ * each field's REAL def QN here (kotlin class properties are minted with
+ * module-level QNs, so composing <class>.<member> would miss the node). */
+ const void *cross_field_map;
} KotlinLSPContext;
/* Initialize a KotlinLSPContext for processing one file. */
@@ -183,8 +197,14 @@ void cbm_run_kotlin_lsp(CBMArena *arena, CBMFileResult *result, const char *sour
* 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`. */
+/* out_field_map (optional): receives ownership of the receiver-QN -> field
+ * list map built during registration (opaque CBMHashTable; the caller frees
+ * with cbm_ht_free). Pass NULL to discard it. The forward declaration lives at
+ * file scope -- a struct tag first named inside a prototype would get
+ * prototype scope and conflict with the foundation typedef. */
+struct CBMHashTable;
void cbm_kotlin_register_lsp_defs(CBMArena *arena, CBMTypeRegistry *reg, const CBMLSPDef *defs,
- int def_count);
+ int def_count, struct CBMHashTable **out_field_map);
void cbm_run_kotlin_lsp_cross(CBMArena *arena, const char *source, int source_len,
const char *module_qn, CBMLSPDef *defs, int def_count,
diff --git a/src/pipeline/pass_lsp_cross.c b/src/pipeline/pass_lsp_cross.c
index 79c45fe60..4df3736c9 100644
--- a/src/pipeline/pass_lsp_cross.c
+++ b/src/pipeline/pass_lsp_cross.c
@@ -105,7 +105,13 @@ static const char *pxc_map_label(const char *label) {
if (!label)
return NULL;
if (cbm_label_is_type_like(label) || strcmp(label, "Protocol") == 0 ||
- strcmp(label, "Function") == 0 || strcmp(label, "Method") == 0) {
+ strcmp(label, "Function") == 0 || strcmp(label, "Method") == 0 ||
+ /* Properties reach the cross defs so the Kotlin registrar can attach
+ * them as fields of their receiver type (kotlin_lookup_property_type
+ * was blind across files, leaving property references to the
+ * name-only fallback). Every registrar filters by explicit label, so
+ * the other languages ignore Variable defs untouched. */
+ strcmp(label, "Variable") == 0) {
return label;
}
return NULL;
diff --git a/tests/repro/repro_lsp_ordered_signatures.c b/tests/repro/repro_lsp_ordered_signatures.c
index 88086f61c..89e61879b 100644
--- a/tests/repro/repro_lsp_ordered_signatures.c
+++ b/tests/repro/repro_lsp_ordered_signatures.c
@@ -104,7 +104,7 @@ static CBMTypeRegistry *build_kotlin_cross_registry_for_contract(CBMArena *arena
int def_count) {
CBMTypeRegistry *registry = alloc_test_registry(arena);
if (registry)
- cbm_kotlin_register_lsp_defs(arena, registry, defs, def_count);
+ cbm_kotlin_register_lsp_defs(arena, registry, defs, def_count, NULL);
return registry;
}