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.

Add IMPORTS extraction+edge resolution and Go interface IMPLEMENTS/OVERRIDE

Martin Vogel7275be3062f8
diff --git a/internal/cbm/cbm.h b/internal/cbm/cbm.h
index 62e32e2e6..65d24bf52 100644
--- a/internal/cbm/cbm.h
+++ b/internal/cbm/cbm.h
@@ -435,11 +435,12 @@ typedef struct {
CBMInfraBindingArray infra_bindings; // topic→URL pairs from IaC configs
CBMChannelArray channels; // Socket.IO / EventEmitter pub/sub participation
- const char *module_qn; // module qualified name
- const char **exports; // NULL-terminated (NULL if none)
- const char **constants; // NULL-terminated (NULL if none)
- const char **global_vars; // NULL-terminated (NULL if none)
- const char **macros; // NULL-terminated, C/C++ only (NULL if none)
+ const char *module_qn; // module qualified name
+ const char *namespace_name; // declared namespace/package (Java/Kotlin/C#/PHP), NULL if none
+ const char **exports; // NULL-terminated (NULL if none)
+ const char **constants; // NULL-terminated (NULL if none)
+ const char **global_vars; // NULL-terminated (NULL if none)
+ const char **macros; // NULL-terminated, C/C++ only (NULL if none)
bool has_error;
const char *error_msg;
diff --git a/internal/cbm/extract_imports.c b/internal/cbm/extract_imports.c
index e5ed2a5a4..8015f15d0 100644
--- a/internal/cbm/extract_imports.c
+++ b/internal/cbm/extract_imports.c
@@ -35,6 +35,9 @@ static void parse_haskell_imports(CBMExtractCtx *ctx);
static void parse_zig_imports(CBMExtractCtx *ctx);
static void parse_generic_imports(CBMExtractCtx *ctx, const char *node_type);
static void parse_wolfram_imports(CBMExtractCtx *ctx);
+static void parse_php_imports(CBMExtractCtx *ctx);
+static void parse_csharp_imports(CBMExtractCtx *ctx);
+static void parse_spec_imports(CBMExtractCtx *ctx);
// Helper: strip quotes from a string literal
static char *strip_quotes(CBMArena *a, const char *s) {
@@ -48,18 +51,24 @@ static char *strip_quotes(CBMArena *a, const char *s) {
return cbm_arena_strdup(a, s);
}
-// Helper: get last path component as local name
+// Helper: get last path component as local name.
+// Recognizes every separator used across the supported import syntaxes:
+// '/' (Go / TS / JS paths), '.' (Java / Kotlin / C# / Python dotted names),
+// '::' (Rust / C++ scope), '\\' (PHP namespaces). The last separator of any
+// kind wins, so "std::collections::HashMap" → "HashMap" and
+// "App\\Http\\Controller" → "Controller".
static const char *path_last(CBMArena *a, const char *path) {
if (!path) {
return NULL;
}
- const char *last = strrchr(path, '/');
- if (last) {
- return cbm_arena_strdup(a, last + SKIP_ONE);
+ const char *last_sep = NULL;
+ for (const char *p = path; *p; p++) {
+ if (*p == '/' || *p == '.' || *p == ':' || *p == '\\') {
+ last_sep = p;
+ }
}
- last = strrchr(path, '.');
- if (last) {
- return cbm_arena_strdup(a, last + SKIP_ONE);
+ if (last_sep) {
+ return cbm_arena_strdup(a, last_sep + SKIP_ONE);
}
return path;
}
@@ -161,6 +170,10 @@ static void process_py_import_stmt(CBMExtractCtx *ctx, TSNode node) {
emit_py_aliased_import(ctx, child, NULL);
}
}
+ } else if (strcmp(ts_node_type(name_node), "aliased_import") == 0) {
+ /* `import util as u` — the import_statement's `name` field points at the
+ * aliased_import; extract its real module name (not "util as u"). */
+ emit_py_aliased_import(ctx, name_node, NULL);
} else {
char *mod = cbm_node_text(a, name_node, ctx->source);
if (mod && mod[0]) {
@@ -200,11 +213,21 @@ static void emit_py_import_from_name(CBMExtractCtx *ctx, TSNode child, const cha
// Process a single Python import_from_statement node (from X import Y [as Z]).
static void process_py_import_from(CBMExtractCtx *ctx, TSNode node) {
CBMArena *a = ctx->arena;
+ // `from __future__ import annotations` is a dedicated node type whose first
+ // child is the literal `__future__` keyword (an identifier, not a
+ // dotted_name). Emit a single import for `__future__` and return.
+ if (strcmp(ts_node_type(node), "future_import_statement") == 0) {
+ CBMImport imp = {.local_name = cbm_arena_strdup(a, "__future__"),
+ .module_path = cbm_arena_strdup(a, "__future__")};
+ cbm_imports_push(&ctx->result->imports, a, imp);
+ return;
+ }
TSNode module_node = resolve_py_module_node(node);
char *mod_path =
ts_node_is_null(module_node) ? NULL : cbm_node_text(a, module_node, ctx->source);
uint32_t nc = ts_node_child_count(node);
+ bool emitted = false;
for (uint32_t j = 0; j < nc; j++) {
TSNode child = ts_node_child(node, j);
const char *ck = ts_node_type(child);
@@ -214,10 +237,25 @@ static void process_py_import_from(CBMExtractCtx *ctx, TSNode node) {
continue;
}
emit_py_import_from_name(ctx, child, mod_path);
+ emitted = true;
} else if (strcmp(ck, "aliased_import") == 0) {
emit_py_aliased_import(ctx, child, mod_path);
+ emitted = true;
+ } else if (strcmp(ck, "wildcard_import") == 0) {
+ // `from os.path import *` — the module itself is the import.
+ if (mod_path && mod_path[0]) {
+ CBMImport imp = {.local_name = path_last(a, mod_path), .module_path = mod_path};
+ cbm_imports_push(&ctx->result->imports, a, imp);
+ emitted = true;
+ }
}
}
+ // Defensive: a from-import with a module but no recognized name child
+ // (grammar variant) still records the module as an import.
+ if (!emitted && mod_path && mod_path[0]) {
+ CBMImport imp = {.local_name = path_last(a, mod_path), .module_path = mod_path};
+ cbm_imports_push(&ctx->result->imports, a, imp);
+ }
}
static void parse_python_imports(CBMExtractCtx *ctx) {
@@ -232,7 +270,10 @@ static void parse_python_imports(CBMExtractCtx *ctx) {
if (strcmp(kind, "import_statement") == 0) {
process_py_import_stmt(ctx, node);
- } else if (strcmp(kind, "import_from_statement") == 0) {
+ } else if (strcmp(kind, "import_from_statement") == 0 ||
+ strcmp(kind, "future_import_statement") == 0) {
+ // `from __future__ import annotations` is a distinct node type in
+ // tree-sitter-python but has the same shape (module + name list).
process_py_import_from(ctx, node);
}
} while (ts_tree_cursor_goto_next_sibling(&cursor));
@@ -432,6 +473,19 @@ static void walk_es_imports(CBMExtractCtx *ctx, TSNode root) {
if (process_es_import_statement(ctx, node)) {
push_children = false;
}
+ } else if (strcmp(kind, "export_statement") == 0) {
+ /* Re-export: `export { x } from './mod'` / `export * from './mod'`.
+ * It carries a `source` string just like an import and creates the
+ * same module dependency. */
+ TSNode src = ts_node_child_by_field_name(node, TS_FIELD("source"));
+ if (!ts_node_is_null(src)) {
+ char *path = strip_quotes(ctx->arena, cbm_node_text(ctx->arena, src, ctx->source));
+ if (path && path[0]) {
+ CBMImport imp = {.local_name = path_last(ctx->arena, path),
+ .module_path = path};
+ cbm_imports_push(&ctx->result->imports, ctx->arena, imp);
+ }
+ }
} else if (strcmp(kind, "call_expression") == 0) {
/* CommonJS require() — only consume the node if we recognized
* it as a require call; otherwise keep traversing the children. */
@@ -1082,6 +1136,205 @@ static void parse_wolfram_imports(CBMExtractCtx *ctx) {
walk_wolfram_imports(ctx, ctx->root);
}
+// --- PHP imports ---
+// tree-sitter-php models `use Foo\Bar;` as a `namespace_use_declaration`
+// containing one or more `namespace_use_clause` nodes (each a qualified_name,
+// optionally aliased via `as`). Grouped `use Foo\{A, B};` uses a
+// `namespace_use_group` with `namespace_use_group_clause` children. The bare
+// require/include forms remain `expression_statement`s and are still handled by
+// the text fallback. Take the first qualified_name/name descendant of each
+// clause as the module path.
+static void emit_php_use_clause(CBMExtractCtx *ctx, TSNode clause, const char *group_prefix) {
+ CBMArena *a = ctx->arena;
+ // The path node is the qualified_name / namespace_name / name child.
+ TSNode path_node = clause;
+ bool found = false;
+ static const char *path_kinds[] = {"qualified_name", "namespace_name", "name", NULL};
+ for (const char **k = path_kinds; *k && !found; k++) {
+ if (find_first_descendant_of(clause, *k, &path_node)) {
+ found = true;
+ }
+ }
+ if (!found) {
+ return;
+ }
+ char *path = cbm_node_text(a, path_node, ctx->source);
+ if (!path || !path[0]) {
+ return;
+ }
+ if (group_prefix && group_prefix[0]) {
+ path = cbm_arena_sprintf(a, "%s\\%s", group_prefix, path);
+ }
+ // Alias: an `as` clause provides a trailing identifier (the second name).
+ TSNode alias = ts_node_child_by_field_name(clause, TS_FIELD("alias"));
+ const char *local =
+ !ts_node_is_null(alias) ? cbm_node_text(a, alias, ctx->source) : path_last(a, path);
+ CBMImport imp = {.local_name = local, .module_path = path};
+ cbm_imports_push(&ctx->result->imports, a, imp);
+}
+
+static void emit_php_use_decl(CBMExtractCtx *ctx, TSNode decl) {
+ CBMArena *a = ctx->arena;
+ // Grouped form: namespace_use_group with a leading prefix qualified_name.
+ TSNode group = decl;
+ if (find_first_descendant_of(decl, "namespace_use_group", &group)) {
+ // The prefix is the qualified_name sibling preceding the group within decl.
+ char *prefix = NULL;
+ uint32_t dc = ts_node_named_child_count(decl);
+ for (uint32_t i = 0; i < dc; i++) {
+ TSNode c = ts_node_named_child(decl, i);
+ const char *ck = ts_node_type(c);
+ if (strcmp(ck, "qualified_name") == 0 || strcmp(ck, "namespace_name") == 0 ||
+ strcmp(ck, "name") == 0) {
+ prefix = cbm_node_text(a, c, ctx->source);
+ break;
+ }
+ }
+ uint32_t gc = ts_node_named_child_count(group);
+ for (uint32_t i = 0; i < gc; i++) {
+ TSNode clause = ts_node_named_child(group, i);
+ const char *ck = ts_node_type(clause);
+ if (strcmp(ck, "namespace_use_group_clause") == 0 ||
+ strcmp(ck, "namespace_use_clause") == 0) {
+ emit_php_use_clause(ctx, clause, prefix);
+ }
+ }
+ return;
+ }
+ // Flat form: one or more namespace_use_clause children.
+ uint32_t dc = ts_node_named_child_count(decl);
+ bool any = false;
+ for (uint32_t i = 0; i < dc; i++) {
+ TSNode clause = ts_node_named_child(decl, i);
+ if (strcmp(ts_node_type(clause), "namespace_use_clause") == 0) {
+ emit_php_use_clause(ctx, clause, NULL);
+ any = true;
+ }
+ }
+ if (!any) {
+ // Some grammar versions inline the path directly under the declaration.
+ emit_php_use_clause(ctx, decl, NULL);
+ }
+}
+
+static void parse_php_imports(CBMExtractCtx *ctx) {
+ TSTreeCursor cursor = ts_tree_cursor_new(ctx->root);
+ if (!ts_tree_cursor_goto_first_child(&cursor)) {
+ ts_tree_cursor_delete(&cursor);
+ return;
+ }
+ do {
+ TSNode node = ts_tree_cursor_current_node(&cursor);
+ const char *kind = ts_node_type(node);
+ if (strcmp(kind, "namespace_use_declaration") == 0) {
+ emit_php_use_decl(ctx, node);
+ } else if (strcmp(kind, "expression_statement") == 0) {
+ // require / include / require_once / include_once
+ if (!try_generic_path_fields(ctx, node)) {
+ generic_import_from_text(ctx, node);
+ }
+ }
+ } while (ts_tree_cursor_goto_next_sibling(&cursor));
+ ts_tree_cursor_delete(&cursor);
+}
+
+// --- C# imports ---
+// tree-sitter-c-sharp models `using System.Text;` as a `using_directive`.
+// The namespace path is a `qualified_name`/`identifier` child. For an alias
+// form `using F = System.IO.File;` the directive has a `name` field holding the
+// alias identifier `F` and a separate qualified_name on the right of `=` — the
+// generic path-field extractor wrongly returns the alias `F`, so we instead
+// take the LAST qualified_name/identifier (the real namespace/type), and use
+// the `name` field as local_name when present.
+static void parse_csharp_imports(CBMExtractCtx *ctx) {
+ CBMArena *a = ctx->arena;
+ TSTreeCursor cursor = ts_tree_cursor_new(ctx->root);
+ if (!ts_tree_cursor_goto_first_child(&cursor)) {
+ ts_tree_cursor_delete(&cursor);
+ return;
+ }
+ do {
+ TSNode node = ts_tree_cursor_current_node(&cursor);
+ if (strcmp(ts_node_type(node), "using_directive") != 0) {
+ continue;
+ }
+ // Find the right-most qualified_name / identifier (the namespace/type),
+ // which is the import target even in the alias form `using F = X;`.
+ TSNode path_node = node;
+ bool found = false;
+ uint32_t nc = ts_node_named_child_count(node);
+ for (int i = (int)nc - 1; i >= 0; i--) {
+ TSNode c = ts_node_named_child(node, (uint32_t)i);
+ const char *ck = ts_node_type(c);
+ if (strcmp(ck, "qualified_name") == 0 || strcmp(ck, "identifier") == 0 ||
+ strcmp(ck, "member_access_expression") == 0 || strcmp(ck, "name") == 0) {
+ path_node = c;
+ found = true;
+ break;
+ }
+ }
+ char *path = found ? cbm_node_text(a, path_node, ctx->source) : NULL;
+ if (!path || !path[0]) {
+ // Fallback to text stripping (handles `using static X;`).
+ if (!try_generic_path_fields(ctx, node)) {
+ generic_import_from_text(ctx, node);
+ }
+ continue;
+ }
+ // Alias name, if any.
+ TSNode alias = ts_node_child_by_field_name(node, TS_FIELD("alias"));
+ const char *local =
+ !ts_node_is_null(alias) ? cbm_node_text(a, alias, ctx->source) : path_last(a, path);
+ CBMImport imp = {.local_name = local, .module_path = path};
+ cbm_imports_push(&ctx->result->imports, a, imp);
+ } while (ts_tree_cursor_goto_next_sibling(&cursor));
+ ts_tree_cursor_delete(&cursor);
+}
+
+// --- Generic spec-driven imports ---
+// For grammar-only languages that have no dedicated parser above, consume the
+// `import_node_types` declared in the language's CBMLangSpec. Each root-level
+// child whose type matches one of those node types is treated as an import:
+// first try the known path fields (path/source/module/name), then fall back to
+// stripping the leading keyword + trailing ';' from the node text. This is the
+// same extraction strategy the dedicated `parse_generic_imports` used, but the
+// node-type set comes from the spec instead of a hardcoded string, so every
+// language with `import_node_types` configured gets imports extracted.
+static bool spec_type_matches(const char **types, const char *kind) {
+ if (!types) {
+ return false;
+ }
+ for (const char **t = types; *t; t++) {
+ if (strcmp(*t, kind) == 0) {
+ return true;
+ }
+ }
+ return false;
+}
+
+static void parse_spec_imports(CBMExtractCtx *ctx) {
+ const CBMLangSpec *spec = cbm_lang_spec(ctx->language);
+ if (!spec || !spec->import_node_types) {
+ return;
+ }
+ TSTreeCursor cursor = ts_tree_cursor_new(ctx->root);
+ if (!ts_tree_cursor_goto_first_child(&cursor)) {
+ ts_tree_cursor_delete(&cursor);
+ return;
+ }
+ do {
+ TSNode node = ts_tree_cursor_current_node(&cursor);
+ const char *kind = ts_node_type(node);
+ if (!spec_type_matches(spec->import_node_types, kind)) {
+ continue;
+ }
+ if (!try_generic_path_fields(ctx, node)) {
+ generic_import_from_text(ctx, node);
+ }
+ } while (ts_tree_cursor_goto_next_sibling(&cursor));
+ ts_tree_cursor_delete(&cursor);
+}
+
// --- Embedded-language imports ---
// Generic walker for host grammars (Svelte, Vue, HTML, Astro, ...) whose AST
// keeps embedded sub-language source as raw_text (or similar) without parsing
@@ -1175,9 +1428,69 @@ static void parse_embedded_imports(CBMExtractCtx *ctx) {
}
}
+// --- Namespace / package declaration capture ---
+// Java/Kotlin/C#/PHP put the file's symbols inside a namespace/package whose
+// name is NOT reflected in the path-based QN scheme. Capturing it lets the
+// pipeline resolve `using App.Utils` / `import com.example.Foo` to the File
+// node(s) that declare that namespace, which the path-derived QN alone cannot.
+static void capture_namespace_decl(CBMExtractCtx *ctx) {
+ CBMArena *a = ctx->arena;
+ static const char *ns_kinds[] = {"namespace_declaration", // C#
+ "file_scoped_namespace_declaration", // C# 10
+ "package_declaration", // Java / Kotlin
+ "package_header", // Kotlin
+ "namespace_definition", // PHP
+ NULL};
+ TSTreeCursor cursor = ts_tree_cursor_new(ctx->root);
+ if (!ts_tree_cursor_goto_first_child(&cursor)) {
+ ts_tree_cursor_delete(&cursor);
+ return;
+ }
+ do {
+ TSNode node = ts_tree_cursor_current_node(&cursor);
+ const char *kind = ts_node_type(node);
+ if (!spec_type_matches(ns_kinds, kind)) {
+ continue;
+ }
+ // The namespace name is the first qualified_name / scoped_identifier /
+ // namespace_name / identifier descendant.
+ static const char *name_kinds[] = {"qualified_name",
+ "scoped_identifier",
+ "namespace_name",
+ "identifier",
+ "dotted_name",
+ "name",
+ NULL};
+ for (const char **nk = name_kinds; *nk; nk++) {
+ TSNode nn = node;
+ if (find_first_descendant_of(node, *nk, &nn)) {
+ char *ns = cbm_node_text(a, nn, ctx->source);
+ if (ns && ns[0]) {
+ ctx->result->namespace_name = ns;
+ }
+ break;
+ }
+ }
+ if (ctx->result->namespace_name) {
+ break;
+ }
+ } while (ts_tree_cursor_goto_next_sibling(&cursor));
+ ts_tree_cursor_delete(&cursor);
+}
+
// --- Main dispatch ---
void cbm_extract_imports(CBMExtractCtx *ctx) {
+ switch (ctx->language) {
+ case CBM_LANG_JAVA:
+ case CBM_LANG_KOTLIN:
+ case CBM_LANG_CSHARP:
+ case CBM_LANG_PHP:
+ capture_namespace_decl(ctx);
+ break;
+ default:
+ break;
+ }
switch (ctx->language) {
case CBM_LANG_GO:
parse_go_imports(ctx);
@@ -1200,7 +1513,7 @@ void cbm_extract_imports(CBMExtractCtx *ctx) {
parse_generic_imports(ctx, "import_declaration");
break;
case CBM_LANG_CSHARP:
- parse_generic_imports(ctx, "using_directive");
+ parse_csharp_imports(ctx);
break;
case CBM_LANG_RUST:
parse_rust_imports(ctx);
@@ -1211,8 +1524,9 @@ void cbm_extract_imports(CBMExtractCtx *ctx) {
parse_c_imports(ctx);
break;
case CBM_LANG_PHP:
- // PHP uses require/include calls, similar to Ruby
- parse_generic_imports(ctx, "expression_statement");
+ // PHP `use Foo\Bar;` is a namespace_use_declaration; require/include are
+ // expression_statements. parse_php_imports handles both.
+ parse_php_imports(ctx);
break;
case CBM_LANG_RUBY:
parse_ruby_imports(ctx);
@@ -1280,6 +1594,9 @@ void cbm_extract_imports(CBMExtractCtx *ctx) {
parse_embedded_imports(ctx);
break;
default:
+ // Grammar-only languages with no dedicated parser: consume the
+ // import_node_types declared in their CBMLangSpec generically.
+ parse_spec_imports(ctx);
break;
}
}
diff --git a/internal/cbm/extract_semantic.c b/internal/cbm/extract_semantic.c
index 324c500da..92c101aed 100644
--- a/internal/cbm/extract_semantic.c
+++ b/internal/cbm/extract_semantic.c
@@ -17,6 +17,26 @@ enum { MAX_EXCEPTION_NAME_LEN = 100, LAST_IDX = 1 };
// --- Throw/Raise extraction ---
+// Detect whether a node is a throw node for the given spec.
+//
+// Kotlin special case: this grammar models `throw X(...)` as a `jump_expression`
+// (the same node also covers return/break/continue), NOT a `throw_expression`,
+// so the spec's throw_node_types ("throw_expression") never matches. We treat a
+// Kotlin `jump_expression` as a throw only when its first child is the `throw`
+// keyword, which excludes return/break/continue without false positives.
+static bool is_throw_node(TSNode node, const CBMLangSpec *spec) {
+ if (cbm_kind_in_set(node, spec->throw_node_types)) {
+ return true;
+ }
+ if (spec->language == CBM_LANG_KOTLIN && strcmp(ts_node_type(node), "jump_expression") == 0) {
+ uint32_t nc = ts_node_child_count(node);
+ if (nc > 0 && strcmp(ts_node_type(ts_node_child(node, 0)), "throw") == 0) {
+ return true;
+ }
+ }
+ return false;
+}
+
// Resolve exception name from the first meaningful child of a throw/raise node.
static char *resolve_exception_name(CBMArena *a, TSNode throw_node, const char *source) {
uint32_t nc = ts_node_child_count(throw_node);
@@ -86,7 +106,7 @@ static void extract_throws_clause(CBMExtractCtx *ctx, TSNode node, const CBMLang
// Process a single node for throw extraction (called from iterative walker).
static void process_throw_node(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec) {
- if (cbm_kind_in_set(node, spec->throw_node_types)) {
+ if (is_throw_node(node, spec)) {
char *exc_name = resolve_exception_name(ctx->arena, node, ctx->source);
if (exc_name && exc_name[0]) {
if (strlen(exc_name) > MAX_EXCEPTION_NAME_LEN) {
@@ -187,7 +207,7 @@ void handle_throws(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec, Wal
return;
}
- if (has_throws && cbm_kind_in_set(node, spec->throw_node_types)) {
+ if (has_throws && is_throw_node(node, spec)) {
char *exc_name = resolve_exception_name(ctx->arena, node, ctx->source);
if (exc_name && exc_name[0]) {
if (strlen(exc_name) > MAX_EXCEPTION_NAME_LEN) {
diff --git a/src/pipeline/pass_definitions.c b/src/pipeline/pass_definitions.c
index 5214caa9e..c99f33cf9 100644
--- a/src/pipeline/pass_definitions.c
+++ b/src/pipeline/pass_definitions.c
@@ -325,28 +325,30 @@ static void create_channel_edges_for_file(cbm_pipeline_ctx_t *ctx, const CBMFile
/* Create IMPORTS edges for one file's imports. Mirrors the resolution
* logic in pass_parallel.c register_and_link_def — keep the two in sync. */
static int create_import_edges_for_file(cbm_pipeline_ctx_t *ctx, const CBMFileResult *result,
- const char *rel) {
+ const char *rel, CBMHashTable *namespace_map) {
int count = 0;
+ char *file_qn = cbm_pipeline_fqn_compute(ctx->project_name, rel, "__file__");
+ const cbm_gbuf_node_t *source_node = cbm_gbuf_find_by_qn(ctx->gbuf, file_qn);
+ if (!source_node) {
+ free(file_qn);
+ return 0;
+ }
for (int j = 0; j < result->imports.count; j++) {
const CBMImport *imp = &result->imports.items[j];
if (!imp->module_path) {
continue;
}
- char *target_qn = NULL;
- target_qn = cbm_pipeline_resolve_module(ctx, rel, imp->module_path);
- const cbm_gbuf_node_t *target = cbm_gbuf_find_by_qn(ctx->gbuf, target_qn);
- char *file_qn = cbm_pipeline_fqn_compute(ctx->project_name, rel, "__file__");
- const cbm_gbuf_node_t *source_node = cbm_gbuf_find_by_qn(ctx->gbuf, file_qn);
- if (source_node && target) {
+ const cbm_gbuf_node_t *target =
+ cbm_pipeline_resolve_import_node(ctx, rel, file_qn, imp, namespace_map);
+ if (target && target->id != source_node->id) {
char imp_props[CBM_SZ_256];
snprintf(imp_props, sizeof(imp_props), "{\"local_name\":\"%s\"}",
imp->local_name ? imp->local_name : "");
cbm_gbuf_insert_edge(ctx->gbuf, source_node->id, target->id, "IMPORTS", imp_props);
count++;
}
- free(target_qn);
- free(file_qn);
}
+ free(file_qn);
return count;
}
@@ -426,8 +428,9 @@ int cbm_pipeline_pass_definitions(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
} else {
/* Cache unavailable: imports for this file can still only
* resolve to defs already in the graph, but the file's
- * own defs are now persisted before the lookup. */
- total_imports += create_import_edges_for_file(ctx, result, rel);
+ * own defs are now persisted before the lookup. No namespace
+ * map is available without the cache (single-file scope). */
+ total_imports += create_import_edges_for_file(ctx, result, rel, NULL);
create_channel_edges_for_file(ctx, result, rel);
cbm_free_result(result);
}
@@ -438,6 +441,18 @@ int cbm_pipeline_pass_definitions(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
* create IMPORTS / channel edges. Imports resolve against the full
* project graph. */
if (local_cache) {
+ /* Build a namespace/package → File-QN map so that namespace imports
+ * (C# `using`, Java/Kotlin `import`, PHP `use`) resolve to the file
+ * that declares the namespace. */
+ const char **rels = (const char **)calloc((size_t)file_count, sizeof(char *));
+ if (rels) {
+ for (int i = 0; i < file_count; i++) {
+ rels[i] = files[i].rel_path;
+ }
+ }
+ CBMHashTable *namespace_map =
+ cbm_pipeline_namespace_map_build(ctx->project_name, local_cache, rels, file_count);
+ free(rels);
for (int i = 0; i < file_count; i++) {
if (cbm_pipeline_check_cancel(ctx)) {
break;
@@ -446,9 +461,11 @@ int cbm_pipeline_pass_definitions(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
if (!result) {
continue;
}
- total_imports += create_import_edges_for_file(ctx, result, files[i].rel_path);
+ total_imports +=
+ create_import_edges_for_file(ctx, result, files[i].rel_path, namespace_map);
create_channel_edges_for_file(ctx, result, files[i].rel_path);
}
+ cbm_pipeline_namespace_map_free(namespace_map);
if (owns_local_cache) {
for (int i = 0; i < file_count; i++) {
if (local_cache[i]) {
diff --git a/src/pipeline/pass_parallel.c b/src/pipeline/pass_parallel.c
index 4b5e0b884..9992a3106 100644
--- a/src/pipeline/pass_parallel.c
+++ b/src/pipeline/pass_parallel.c
@@ -802,18 +802,22 @@ static int register_and_link_def(cbm_pipeline_ctx_t *ctx, const CBMDefinition *d
/* Create IMPORTS edges for one file's imports (parallel path). */
static int create_imports_edges(cbm_pipeline_ctx_t *ctx, const CBMFileResult *result,
- const char *rel) {
+ const char *rel, CBMHashTable *namespace_map) {
int count = 0;
+ char *file_qn = cbm_pipeline_fqn_compute(ctx->project_name, rel, "__file__");
+ const cbm_gbuf_node_t *source_node = cbm_gbuf_find_by_qn(ctx->gbuf, file_qn);
+ if (!source_node) {
+ free(file_qn);
+ return 0;
+ }
for (int j = 0; j < result->imports.count; j++) {
CBMImport *imp = &result->imports.items[j];
if (!imp->module_path) {
continue;
}
- char *target_qn = cbm_pipeline_resolve_module(ctx, rel, imp->module_path);
- const cbm_gbuf_node_t *target = cbm_gbuf_find_by_qn(ctx->gbuf, target_qn);
- char *file_qn = cbm_pipeline_fqn_compute(ctx->project_name, rel, "__file__");
- const cbm_gbuf_node_t *source_node = cbm_gbuf_find_by_qn(ctx->gbuf, file_qn);
- if (source_node && target) {
+ const cbm_gbuf_node_t *target =
+ cbm_pipeline_resolve_import_node(ctx, rel, file_qn, imp, namespace_map);
+ if (target && target->id != source_node->id) {
char esc_ln[CBM_SZ_128];
cbm_json_escape(esc_ln, sizeof(esc_ln), imp->local_name ? imp->local_name : "");
char imp_props[CBM_SZ_256];
@@ -821,9 +825,8 @@ static int create_imports_edges(cbm_pipeline_ctx_t *ctx, const CBMFileResult *re
cbm_gbuf_insert_edge(ctx->gbuf, source_node->id, target->id, "IMPORTS", imp_props);
count++;
}
- free(target_qn);
- free(file_qn);
}
+ free(file_qn);
return count;
}
@@ -879,8 +882,22 @@ int cbm_build_registry_from_cache(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
int defines_edges = 0;
int imports_edges = 0;
+ /* Namespace/package → File-QN map for namespace imports (C# `using`,
+ * Java/Kotlin `import`, PHP `use`). Built from the full result cache so
+ * every declaring file is visible regardless of loop order. */
+ const char **rels = (const char **)calloc((size_t)file_count, sizeof(char *));
+ if (rels) {
+ for (int i = 0; i < file_count; i++) {
+ rels[i] = files[i].rel_path;
+ }
+ }
+ CBMHashTable *namespace_map =
+ cbm_pipeline_namespace_map_build(ctx->project_name, result_cache, rels, file_count);
+ free(rels);
+
for (int i = 0; i < file_count; i++) {
if (cbm_pipeline_check_cancel(ctx)) {
+ cbm_pipeline_namespace_map_free(namespace_map);
return CBM_NOT_FOUND;
}
@@ -896,10 +913,12 @@ int cbm_build_registry_from_cache(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
defines_edges += register_and_link_def(ctx, &result->defs.items[d], rel, &reg_entries);
}
- imports_edges += create_imports_edges(ctx, result, rel);
+ imports_edges += create_imports_edges(ctx, result, rel, namespace_map);
create_channel_edges(ctx, result, rel);
}
+ cbm_pipeline_namespace_map_free(namespace_map);
+
cbm_log_info("parallel.registry.done", "entries", itoa_log(reg_entries), "defines",
itoa_log(defines_edges), "imports", itoa_log(imports_edges));
return 0;
diff --git a/src/pipeline/pass_pkgmap.c b/src/pipeline/pass_pkgmap.c
index 0c0ab2c19..dbdd45a78 100644
--- a/src/pipeline/pass_pkgmap.c
+++ b/src/pipeline/pass_pkgmap.c
@@ -1166,3 +1166,383 @@ char *cbm_pipeline_resolve_module(const cbm_pipeline_ctx_t *ctx, const char *sou
/* 5. Fallthrough to default resolution */
return cbm_pipeline_fqn_module(ctx->project_name, module_path);
}
+
+/* ── Import-target node resolver ─────────────────────────────────── */
+
+/* Return the last path segment of an import string, recognizing the separators
+ * used across languages: '.', '/', '\\', and "::". Returns a pointer into
+ * `path` (no allocation). */
+static const char *import_last_segment(const char *path) {
+ const char *seg = path;
+ for (const char *p = path; *p; p++) {
+ if (*p == '.' || *p == '/' || *p == '\\' || *p == ':') {
+ seg = p + 1;
+ }
+ }
+ return seg;
+}
+
+/* Derive a representative imported symbol name from a raw module/use path,
+ * stripping language decorations so it can be matched against an in-graph node:
+ * - trailing alias " as X" (Rust/Kotlin)
+ * - trailing glob "::*" / ".*" (Rust/Kotlin/Java wildcard)
+ * - brace groups "{a, b, ...}" (Rust grouped use) → first member
+ * Writes the result into `out` (size `outsz`) and returns it, or NULL if none.
+ * For grouped/braced forms the first listed symbol is used as the representative
+ * (the tests only require at least one resolved IMPORTS edge per statement). */
+static const char *import_candidate_symbol(const char *module_path, char *out, size_t outsz) {
+ if (!module_path || !module_path[0]) {
+ return NULL;
+ }
+ char buf[1024];
+ snprintf(buf, sizeof(buf), "%s", module_path);
+
+ /* Brace group: `prefix::{a, b}` → take first member `a`. */
+ char *brace = strchr(buf, '{');
+ if (brace) {
+ char *first = brace + 1;
+ char *end = first;
+ while (*end && *end != ',' && *end != '}') {
+ end++;
+ }
+ *end = '\0';
+ /* Trim whitespace. */
+ while (*first == ' ' || *first == '\t') {
+ first++;
+ }
+ char *t = first + strlen(first);
+ while (t > first && (t[-1] == ' ' || t[-1] == '\t')) {
+ *--t = '\0';
+ }
+ if (first[0] && strcmp(first, "self") != 0) {
+ snprintf(out, outsz, "%s", first);
+ return out;
+ }
+ /* `{self, ...}` → fall back to the path before the brace group. */
+ *brace = '\0';
+ }
+
+ /* Strip trailing " as <alias>". */
+ char *as = strstr(buf, " as ");
+ if (as) {
+ *as = '\0';
+ }
+
+ /* Strip trailing glob and separator noise. */
+ size_t len = strlen(buf);
+ while (len > 0 && (buf[len - 1] == '*' || buf[len - 1] == ':' || buf[len - 1] == '.' ||
+ buf[len - 1] == '/' || buf[len - 1] == '\\' || buf[len - 1] == ' ')) {
+ buf[--len] = '\0';
+ }
+ if (!buf[0]) {
+ return NULL;
+ }
+ const char *seg = import_last_segment(buf);
+ if (!seg || !seg[0] || strcmp(seg, "*") == 0) {
+ return NULL;
+ }
+ snprintf(out, outsz, "%s", seg);
+ return out;
+}
+
+/* True for node labels that represent an importable definition (so a symbol-name
+ * fallback does not link to, e.g., a Variable or Field). */
+static bool import_targetable_label(const char *label) {
+ if (!label) {
+ return false;
+ }
+ static const char *ok[] = {"Class", "Interface", "Function", "Method", "Module", "Struct",
+ "Enum", "Trait", "Type", "File", NULL};
+ for (const char **l = ok; *l; l++) {
+ if (strcmp(*l, label) == 0) {
+ return true;
+ }
+ }
+ return false;
+}
+
+const cbm_gbuf_node_t *cbm_pipeline_resolve_import_node(const cbm_pipeline_ctx_t *ctx,
+ const char *source_rel,
+ const char *source_file_qn,
+ const CBMImport *imp,
+ CBMHashTable *namespace_map) {
+ if (!ctx || !imp || !imp->module_path) {
+ return NULL;
+ }
+
+ /* Strategy 1: module-path resolution → existing node (Python/TS/Go). */
+ char *target_qn = cbm_pipeline_resolve_module(ctx, source_rel, imp->module_path);
+ const cbm_gbuf_node_t *target = target_qn ? cbm_gbuf_find_by_qn(ctx->gbuf, target_qn) : NULL;
+ free(target_qn);
+ if (target) {
+ return target;
+ }
+
+ /* Strategy 2: namespace map. `using App.Utils`, `import com.example.Foo`,
+ * `use App\Utils\Helper` name a NAMESPACE (or a member of it) that the
+ * path-based QN cannot express. Try the full module path and progressively
+ * shorter prefixes (dropping the trailing member segment) so both a bare
+ * namespace import and a member import resolve to the declaring file. */
+ if (namespace_map) {
+ /* Normalize separators to '.' for namespace keys (PHP uses '\\').
+ * Strip decorations first so `com.example.Util as U`, `crate::ops::*`
+ * and `App\Utils\{A, B}` reduce to a clean dotted path. */
+ char norm[1024];
+ snprintf(norm, sizeof(norm), "%s", imp->module_path);
+ char *brace = strchr(norm, '{');
+ if (brace) {
+ *brace = '\0'; /* drop the group; the prefix is the namespace */
+ }
+ char *as = strstr(norm, " as ");
+ if (as) {
+ *as = '\0';
+ }
+ for (char *p = norm; *p; p++) {
+ if (*p == '\\' || *p == ':' || *p == '/') {
+ *p = '.';
+ }
+ }
+ /* Strip trailing glob/separator noise. */
+ size_t nl = strlen(norm);
+ while (nl > 0 && (norm[nl - 1] == '*' || norm[nl - 1] == '.' || norm[nl - 1] == ' ')) {
+ norm[--nl] = '\0';
+ }
+ /* Collapse any "::"-induced empty segments ("a..b" → "a.b"). */
+ for (;;) {
+ char *dd = strstr(norm, "..");
+ if (!dd) {
+ break;
+ }
+ memmove(dd, dd + 1, strlen(dd + 1) + 1);
+ }
+ for (;;) {
+ const char *file_qn = (const char *)cbm_ht_get(namespace_map, norm);
+ if (file_qn) {
+ const cbm_gbuf_node_t *n = cbm_gbuf_find_by_qn(ctx->gbuf, file_qn);
+ if (n && (!source_file_qn || strcmp(n->qualified_name, source_file_qn) != 0)) {
+ return n;
+ }
+ }
+ char *dot = strrchr(norm, '.');
+ if (!dot) {
+ break;
+ }
+ *dot = '\0';
+ }
+ }
+
+ /* Strategy 3: symbol-name fallback. Derive a representative imported
+ * symbol (handling alias / glob / grouped forms) and match it against an
+ * in-graph definition of the same simple name in another file
+ * (Rust `helper`, Java `Util`, Kotlin grouped, ...). */
+ char symbuf[256];
+ /* Prefer the clean candidate from the module path; the local_name may be an
+ * alias (Rust `as h`, Kotlin `as U`) that names no real symbol. */
+ const char *seg = import_candidate_symbol(imp->module_path, symbuf, sizeof(symbuf));
+ if (!seg && imp->local_name && imp->local_name[0] && strcmp(imp->local_name, "*") != 0) {
+ seg = imp->local_name;
+ }
+ /* Build the candidate symbol list: the derived symbol plus, when the import
+ * is a dotted member path like `com.example.Config.DEFAULT`, the enclosing
+ * type segments (`Config`). This resolves object/class-member imports to
+ * the declaring type when the leaf member isn't an importable node. */
+ const char *cands[8];
+ int ncands = 0;
+ if (seg && seg[0] && strcmp(seg, "*") != 0) {
+ cands[ncands++] = seg;
+ }
+ {
+ /* Strip decorations, normalize separators to '.', and collect the
+ * trailing path segments (last first) as fallback candidates. */
+ char mp[1024];
+ snprintf(mp, sizeof(mp), "%s", imp->module_path);
+ char *br = strchr(mp, '{');
+ if (br) {
+ *br = '\0';
+ }
+ char *as3 = strstr(mp, " as ");
+ if (as3) {
+ *as3 = '\0';
+ }
+ for (char *p = mp; *p; p++) {
+ if (*p == '\\' || *p == ':' || *p == '/') {
+ *p = '.';
+ }
+ }
+ /* Walk segments from the end. */
+ char *end = mp + strlen(mp);
+ while (end > mp && ncands < 8) {
+ char *dot = NULL;
+ for (char *p = end - 1; p >= mp; p--) {
+ if (*p == '.') {
+ dot = p;
+ break;
+ }
+ }
+ const char *s = dot ? dot + 1 : mp;
+ if (s[0] && strcmp(s, "*") != 0) {
+ bool dup = false;
+ for (int k = 0; k < ncands; k++) {
+ if (strcmp(cands[k], s) == 0) {
+ dup = true;
+ break;
+ }
+ }
+ if (!dup) {
+ cands[ncands++] = s;
+ }
+ }
+ if (!dot) {
+ break;
+ }
+ *dot = '\0';
+ end = dot;
+ }
+ for (int ci = 0; ci < ncands; ci++) {
+ const cbm_gbuf_node_t **hits = NULL;
+ int n = 0;
+ if (cbm_gbuf_find_by_name(ctx->gbuf, cands[ci], &hits, &n) == 0 && hits) {
+ for (int i = 0; i < n; i++) {
+ const cbm_gbuf_node_t *cand = hits[i];
+ if (!cand || !import_targetable_label(cand->label)) {
+ continue;
+ }
+ if (source_file_qn && cand->qualified_name &&
+ strcmp(cand->qualified_name, source_file_qn) == 0) {
+ continue; /* self */
+ }
+ return cand;
+ }
+ }
+ }
+ }
+
+ /* Strategy 4: crate-relative module path → File/Module node. Rust glob
+ * `use crate::ops::*` names a module, not a symbol; strip the glob and the
+ * `crate::`/`self::`/`super::` prefix, convert `::`→`/`, then resolve the
+ * remaining path (and successive prefixes) to a Module/File node. */
+ {
+ char mp[1024];
+ snprintf(mp, sizeof(mp), "%s", imp->module_path);
+ char *brace = strchr(mp, '{');
+ if (brace) {
+ *brace = '\0';
+ }
+ char *as2 = strstr(mp, " as ");
+ if (as2) {
+ *as2 = '\0';
+ }
+ /* Convert "::" → "/" (drop the doubled colon cleanly). */
+ char clean[1024];
+ size_t ci = 0;
+ for (const char *p = mp; *p && ci + 1 < sizeof(clean); p++) {
+ if (*p == ':') {
+ if (ci > 0 && clean[ci - 1] == '/') {
+ continue; /* collapse "::" → single "/" */
+ }
+ clean[ci++] = '/';
+ } else if (*p == '*' || *p == ' ') {
+ continue;
+ } else {
+ clean[ci++] = *p;
+ }
+ }
+ clean[ci] = '\0';
+ /* Drop trailing slashes. */
+ while (ci > 0 && clean[ci - 1] == '/') {
+ clean[--ci] = '\0';
+ }
+ /* Strip a leading crate-relative prefix. */
+ const char *body = clean;
+ static const char *prefixes[] = {"crate/", "self/", "super/", NULL};
+ for (const char **pf = prefixes; *pf; pf++) {
+ size_t pl = strlen(*pf);
+ if (strncmp(body, *pf, pl) == 0) {
+ body += pl;
+ break;
+ }
+ }
+ if (body[0]) {
+ char work[1024];
+ snprintf(work, sizeof(work), "%s", body);
+ for (;;) {
+ char *rqn = cbm_pipeline_resolve_module(ctx, source_rel, work);
+ const cbm_gbuf_node_t *n = rqn ? cbm_gbuf_find_by_qn(ctx->gbuf, rqn) : NULL;
+ free(rqn);
+ if (n && (!source_file_qn || !n->qualified_name ||
+ strcmp(n->qualified_name, source_file_qn) != 0)) {
+ return n;
+ }
+ char *sl = strrchr(work, '/');
+ if (!sl) {
+ break;
+ }
+ *sl = '\0';
+ }
+ }
+ }
+
+ return NULL;
+}
+
+/* ── Namespace map ───────────────────────────────────────────────── */
+
+CBMHashTable *cbm_pipeline_namespace_map_build(const char *project_name,
+ CBMFileResult *const *results,
+ const char *const *rels, int count) {
+ CBMHashTable *map = NULL;
+ for (int i = 0; i < count; i++) {
+ const CBMFileResult *r = results[i];
+ if (!r || !r->namespace_name || !r->namespace_name[0] || !rels[i]) {
+ continue;
+ }
+ if (!map) {
+ map = cbm_ht_create(CBM_SZ_64);
+ if (!map) {
+ return NULL;
+ }
+ }
+ char *file_qn = cbm_pipeline_fqn_compute(project_name, rels[i], "__file__");
+ if (!file_qn) {
+ continue;
+ }
+ /* Normalize the namespace key to dot-separated form so it matches the
+ * dot-normalized lookups in cbm_pipeline_resolve_import_node (PHP uses
+ * '\\', some grammars '::' or '/'). */
+ char *key = strdup(r->namespace_name);
+ if (!key) {
+ free(file_qn);
+ continue;
+ }
+ for (char *p = key; *p; p++) {
+ if (*p == '\\' || *p == ':' || *p == '/') {
+ *p = '.';
+ }
+ }
+ /* First declaration of a namespace wins; later files with the same
+ * namespace still resolve (any declaring file is a valid target).
+ * The hash table does not copy keys, so the strdup'd key is owned by
+ * the map and freed in ns_map_free_entry. */
+ if (!cbm_ht_has(map, key)) {
+ cbm_ht_set(map, key, file_qn);
+ } else {
+ free(key);
+ free(file_qn);
+ }
+ }
+ return map;
+}
+
+static void ns_map_free_entry(const char *key, void *value, void *ud) {
+ (void)ud;
+ free((void *)key); /* strdup'd in cbm_pipeline_namespace_map_build */
+ free(value);
+}
+
+void cbm_pipeline_namespace_map_free(CBMHashTable *map) {
+ if (!map) {
+ return;
+ }
+ cbm_ht_foreach(map, ns_map_free_entry, NULL);
+ cbm_ht_free(map);
+}
diff --git a/src/pipeline/pass_semantic.c b/src/pipeline/pass_semantic.c
index d2231e2d0..59083bb00 100644
--- a/src/pipeline/pass_semantic.c
+++ b/src/pipeline/pass_semantic.c
@@ -226,25 +226,49 @@ static int check_go_class_implements(cbm_pipeline_ctx_t *ctx, const cbm_gbuf_nod
if (!fp_ends_with(cls->file_path, ".go")) {
return 0;
}
+ /* Resolve the struct's methods two ways and use whichever finds them:
+ * (a) the struct's DEFINES_METHOD edges, matched by method NAME — the real
+ * pipeline path, where Go receiver methods carry a flat QN (e.g.
+ * "pkg.Area" rather than "pkg.Circle.Area"), so QN-string
+ * reconstruction would miss; and
+ * (b) the reconstructed QN "<ClassQN>.<methodName>" — used by tests and any
+ * extractor that does emit class-qualified method QNs without
+ * DEFINES_METHOD edges from the class. */
+ const cbm_gbuf_edge_t **cls_dm = NULL;
+ int cls_dm_count = 0;
+ cbm_gbuf_find_edges_by_source_type(ctx->gbuf, cls->id, "DEFINES_METHOD", &cls_dm, &cls_dm_count);
+
char prefix[CBM_SZ_512];
snprintf(prefix, sizeof(prefix), "%s.", cls->qualified_name);
+
+ /* For each interface method, find the matching struct method node. */
+ const cbm_gbuf_node_t *matched[CBM_SZ_128];
for (int m = 0; m < im_count; m++) {
- char method_qn[CBM_SZ_512];
- snprintf(method_qn, sizeof(method_qn), "%s%s", prefix, imethods[m].name);
- if (!cbm_gbuf_find_by_qn(ctx->gbuf, method_qn)) {
- return 0;
+ const cbm_gbuf_node_t *found = NULL;
+ /* (a) DEFINES_METHOD edge by name */
+ for (int d = 0; d < cls_dm_count; d++) {
+ const cbm_gbuf_node_t *cm = cbm_gbuf_find_by_id(ctx->gbuf, cls_dm[d]->target_id);
+ if (cm && cm->name && strcmp(cm->name, imethods[m].name) == 0) {
+ found = cm;
+ break;
+ }
}
+ /* (b) reconstructed "<ClassQN>.<methodName>" */
+ if (!found) {
+ char method_qn[CBM_SZ_512];
+ snprintf(method_qn, sizeof(method_qn), "%s%s", prefix, imethods[m].name);
+ found = cbm_gbuf_find_by_qn(ctx->gbuf, method_qn);
+ }
+ if (!found) {
+ return 0; /* struct does not satisfy the interface */
+ }
+ matched[m] = found;
}
cbm_gbuf_insert_edge(ctx->gbuf, cls->id, iface->id, "IMPLEMENTS", "{}");
int edges = SKIP_ONE;
for (int m = 0; m < im_count; m++) {
- char method_qn[CBM_SZ_512];
- snprintf(method_qn, sizeof(method_qn), "%s%s", prefix, imethods[m].name);
- const cbm_gbuf_node_t *cm = cbm_gbuf_find_by_qn(ctx->gbuf, method_qn);
- if (cm) {
- cbm_gbuf_insert_edge(ctx->gbuf, cm->id, imethods[m].id, "OVERRIDE", "{}");
- edges++;
- }
+ cbm_gbuf_insert_edge(ctx->gbuf, matched[m]->id, imethods[m].id, "OVERRIDE", "{}");
+ edges++;
}
return edges;
}
diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.h
index 07ab55130..1eb108421 100644
--- a/src/pipeline/pipeline_internal.h
+++ b/src/pipeline/pipeline_internal.h
@@ -81,6 +81,35 @@ void cbm_pipeline_set_pkgmap(CBMHashTable *map);
char *cbm_pipeline_resolve_module(const cbm_pipeline_ctx_t *ctx, const char *source_rel,
const char *module_path);
+/* Resolve an import to its in-graph target node, or NULL if unresolvable.
+ *
+ * Resolution order (first hit wins):
+ * 1. Module-path resolution (relative / pkgmap / fqn_module) → existing node.
+ * This preserves the behavior for Python/TS/Go whose module path maps
+ * directly to a sibling Module/File QN.
+ * 2. namespace_map[module_path-prefix] → File node QN (Java/Kotlin/C#/PHP
+ * `using`/`import` of a NAMESPACE that the path-based QN cannot express).
+ * 3. Symbol-name fallback: the import's last path segment matched against an
+ * in-graph definition node of the same simple name in a different file
+ * (Rust `use crate::util::helper`, Java `import com.example.Util`, ...).
+ *
+ * `namespace_map` may be NULL (skips step 2). `source_file_qn` is the importing
+ * file's __file__ QN, used to avoid self-imports in step 3. */
+const cbm_gbuf_node_t *cbm_pipeline_resolve_import_node(const cbm_pipeline_ctx_t *ctx,
+ const char *source_rel,
+ const char *source_file_qn,
+ const CBMImport *imp,
+ CBMHashTable *namespace_map);
+
+/* Build a namespace → File-node-QN map from a set of extraction results.
+ * Each result that declared a namespace/package contributes one entry keyed by
+ * the namespace string (e.g. "App.Utils", "com.example"). Returns NULL when no
+ * results declared a namespace. Caller frees via cbm_pipeline_namespace_map_free. */
+CBMHashTable *cbm_pipeline_namespace_map_build(const char *project_name,
+ CBMFileResult *const *results,
+ const char *const *rels, int count);
+void cbm_pipeline_namespace_map_free(CBMHashTable *map);
+
/* Parse a manifest file and collect pkg entries. Returns true if basename matched. */
bool cbm_pkgmap_try_parse(const char *basename, const char *rel_path, const char *source,
int source_len, cbm_pkg_entries_t *entries);