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(perl-lsp): implement core semantic resolution (package/sub/method/ISA/bless/exports)

Shane McCarronf0aa57c1c6e1
diff --git a/internal/cbm/lsp/perl_lsp.c b/internal/cbm/lsp/perl_lsp.c
index fd72878bd..e5b245523 100644
--- a/internal/cbm/lsp/perl_lsp.c
+++ b/internal/cbm/lsp/perl_lsp.c
@@ -1,29 +1,79 @@
/*
- * perl_lsp.c — Perl Light Semantic Pass (skeleton).
+ * perl_lsp.c — Perl Light Semantic Pass.
*
* In-process type-aware call resolver for Perl. Mirrors the php_lsp.c /
* go_lsp.c shape:
* 1. Build a CBMTypeRegistry from file-local definitions + stdlib
- * (perlfunc builtins + curated CPAN types).
- * 2. Walk top-level: collect `package` declarations, @ISA / `use parent`
- * inheritance, and Exporter-style `use` imports.
- * 3. Walk each sub body, push scope, track bless var→class, and resolve
- * method/function call expressions.
+ * (perlfunc builtins + curated CPAN types) plus a per-package type entry
+ * carrying @ISA parents and the package's sub method table.
+ * 2. perl_lsp_process_file does a TWO-PASS walk:
+ * PASS 1 — collect `package` declarations (a file may switch packages
+ * mid-file), @ISA / `use parent` / `use base` inheritance, and
+ * Exporter-style `use Foo qw(...)` imports.
+ * PASS 2 — walk each `subroutine_declaration_statement`, push a scope,
+ * bind the $self/$class invocant, track bless var→class, and resolve
+ * method/function call expressions into CBMResolvedCall edges.
*
- * This file currently contains only the scaffold: perl_lsp_init plus an
- * inert cbm_run_perl_lsp that runs the three phases as safe no-ops. Real
- * call resolution lands in a later plan (22-03); cbm_run_perl_lsp here MUST
- * NOT emit any resolved-call edges.
+ * Verified tree-sitter-perl node/field names (Open Questions #1-3 in
+ * 22-RESEARCH.md). These were confirmed against the vendored compiled grammar
+ * at internal/cbm/vendored/grammars/perl/parser.c (ts_symbol_names and
+ * ts_field_names tables — no node-types.json/grammar.js is vendored):
+ * - method_call_expression : fields `invocant` (receiver) and `method`
+ * (NOT `object`); arguments under field `arguments`.
+ * - function_call_expression / ambiguous_function_call_expression :
+ * field `function` (callee) and `arguments`.
+ * - package_statement : field `name` (the package name; "::"-separated).
+ * - use_statement : field `module` (the imported module) plus a
+ * `quoted_word_list` child for the `qw(...)` import/parent list.
+ * - assignment_expression : fields `left`, `operator`, `right`.
+ * - variable_declaration : holds an assignment_expression child for the
+ * `my $x = EXPR` initializer.
+ * - scalar/array/hash variables: node types `scalar`, `array`, `hash`
+ * (sigil included in node text, e.g. "$self", "@ISA").
+ * - string literals: `string_literal` / `interpolated_string_literal`;
+ * bare class names: `bareword` / `package` (autoquoted).
+ *
+ * QN scheme (verified against helpers.c cbm_enclosing_func_qn): Perl has no
+ * class_node_types, so the structural extractor names every sub
+ * `module_qn.subname` — the package is NOT woven into the sub QN. This module
+ * therefore matches caller/callee edges by registering each file-local sub
+ * under its extractor QN and resolving calls to those QNs by short name. A
+ * per-package CBMRegisteredType (keyed by the package name) carries
+ * method_names/method_qns + embedded_types (@ISA parents) so method dispatch
+ * can walk the inheritance chain.
+ *
+ * Zero-edge guarantee: if a receiver's type is unknown/unindexed, NO edge is
+ * emitted (false edges are worse than missing edges). Symbol-table aliasing
+ * (*Foo::bar = \&...) is intentionally ignored.
*/
#include "perl_lsp.h"
#include "../helpers.h"
+#include "../arena.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#define PERL_EVAL_MAX_DEPTH 32
+/* Recursion cap for perl_eval_expr_type — mirrors php_eval_expr_type's guard
+ * (php returns unknown at depth >= 8). */
+#define PERL_EVAL_MAX_DEPTH 8
+
+/* bless / constructor confidence levels (22-RESEARCH.md §3). */
+#define PERL_CONF_LITERAL 0.95f /* bless($r, 'Literal'); resolved call */
+#define PERL_CONF_INFERRED 0.75f /* ref($class)||$class idiom */
+
+/* ── forward declarations ───────────────────────────────────────── */
+
+static void perl_resolve_calls_in_node(PerlLSPContext *ctx, TSNode node);
+static void process_subroutine(PerlLSPContext *ctx, TSNode node);
+static void process_package_decl(PerlLSPContext *ctx, TSNode node);
+static void perl_pass1_scan(PerlLSPContext *ctx, TSNode node);
+static const CBMType *perl_eval_function_call_type(PerlLSPContext *ctx, TSNode node);
+static const CBMType *perl_eval_method_call_type(PerlLSPContext *ctx, TSNode node);
+static const CBMType *perl_eval_new_type(PerlLSPContext *ctx, TSNode node);
+static void perl_emit_resolved(PerlLSPContext *ctx, const char *callee_qn, const char *strategy,
+ float confidence);
/* ── helpers ────────────────────────────────────────────────────── */
@@ -55,7 +105,67 @@ static char *perl_pkg_to_dot(CBMArena *a, const char *pkg) {
return out;
}
-/* ── public API ─────────────────────────────────────────────────── */
+/* Strip a leading sigil ($ @ % & *) from a Perl variable's text. Returns a
+ * pointer into the same string (no copy). */
+static const char *perl_strip_sigil(const char *name) {
+ if (!name)
+ return NULL;
+ if (name[0] == '$' || name[0] == '@' || name[0] == '%' || name[0] == '&' || name[0] == '*')
+ return name + 1;
+ return name;
+}
+
+/* Strip surrounding quotes from a string-literal node's text ('...' / "...").
+ * Returns an arena copy of the inner content, or NULL if not quoted. */
+static char *perl_unquote(CBMArena *a, const char *s) {
+ if (!s || !s[0])
+ return NULL;
+ size_t n = strlen(s);
+ if ((s[0] == '\'' || s[0] == '"') && n >= 2 && s[n - 1] == s[0]) {
+ return cbm_arena_strndup(a, s + 1, n - 2);
+ }
+ return NULL;
+}
+
+/* Is this a string-literal-ish node? */
+static bool perl_is_string_node(const char *k) {
+ return strcmp(k, "string_literal") == 0 || strcmp(k, "interpolated_string_literal") == 0;
+}
+
+/* Is this a bareword / package-name node (e.g. a bare class name `Foo::Bar`)? */
+static bool perl_is_bareword_node(const char *k) {
+ return strcmp(k, "bareword") == 0 || strcmp(k, "package") == 0 ||
+ strcmp(k, "autoquoted_bareword") == 0 || strcmp(k, "_bareword") == 0;
+}
+
+/* Extract the declared scalar from a variable_declaration (`my $x`). The
+ * grammar exposes the target via the `variable` field (singular). Returns the
+ * scalar/array/hash node, or the input unchanged if it is not a declaration. */
+static TSNode perl_decl_target(TSNode node) {
+ if (strcmp(ts_node_type(node), "variable_declaration") == 0) {
+ TSNode v = ts_node_child_by_field_name(node, "variable", 8);
+ if (!ts_node_is_null(v))
+ return v;
+ }
+ return node;
+}
+
+/* Find the first named child whose node type is `kind` (shallow). */
+static TSNode perl_first_child_of_type(TSNode node, const char *kind) {
+ uint32_t nc = ts_node_child_count(node);
+ for (uint32_t i = 0; i < nc; i++) {
+ TSNode c = ts_node_child(node, i);
+ if (ts_node_is_null(c) || !ts_node_is_named(c))
+ continue;
+ if (strcmp(ts_node_type(c), kind) == 0)
+ return c;
+ }
+ TSNode null_node;
+ memset(&null_node, 0, sizeof(null_node));
+ return null_node;
+}
+
+/* ── public API: init / use map ─────────────────────────────────── */
void perl_lsp_init(PerlLSPContext *ctx, CBMArena *arena, const char *source, int source_len,
const CBMTypeRegistry *registry, const char *module_qn,
@@ -75,42 +185,1034 @@ void perl_lsp_init(PerlLSPContext *ctx, CBMArena *arena, const char *source, int
}
void perl_lsp_add_use(PerlLSPContext *ctx, const char *local_name, const char *target_qn) {
- /* TODO(plan 22-03): grow use_local_names/use_target_qns and record the
- * mapping. Inert for now so the skeleton emits no edges. */
- (void)ctx;
- (void)local_name;
- (void)target_qn;
-}
-
-void perl_lsp_process_file(PerlLSPContext *ctx, TSNode root) {
- /* TODO(plan 22-03): walk packages, @ISA, `use` imports, and sub bodies,
- * resolving calls into ctx->resolved_calls. Empty walk for the skeleton. */
- (void)ctx;
- (void)root;
+ if (!ctx || !local_name || !target_qn)
+ return;
+ if (ctx->use_count >= ctx->use_cap) {
+ int newcap = ctx->use_cap ? ctx->use_cap * 2 : 8;
+ const char **ln =
+ (const char **)cbm_arena_alloc(ctx->arena, (size_t)newcap * sizeof(char *));
+ const char **tq =
+ (const char **)cbm_arena_alloc(ctx->arena, (size_t)newcap * sizeof(char *));
+ if (!ln || !tq)
+ return;
+ for (int i = 0; i < ctx->use_count; i++) {
+ ln[i] = ctx->use_local_names[i];
+ tq[i] = ctx->use_target_qns[i];
+ }
+ ctx->use_local_names = ln;
+ ctx->use_target_qns = tq;
+ ctx->use_cap = newcap;
+ }
+ ctx->use_local_names[ctx->use_count] = cbm_arena_strdup(ctx->arena, local_name);
+ ctx->use_target_qns[ctx->use_count] = cbm_arena_strdup(ctx->arena, target_qn);
+ ctx->use_count++;
}
-const CBMType *perl_eval_expr_type(PerlLSPContext *ctx, TSNode node) {
- /* TODO(plan 22-03): evaluate expression types. */
- (void)ctx;
- (void)node;
- return cbm_type_unknown();
+/* Look up an Exporter import: local symbol → target QN, or NULL. */
+static const char *perl_find_import(PerlLSPContext *ctx, const char *local_name) {
+ for (int i = 0; i < ctx->use_count; i++) {
+ if (strcmp(ctx->use_local_names[i], local_name) == 0)
+ return ctx->use_target_qns[i];
+ }
+ return NULL;
}
const char *perl_resolve_package_name(PerlLSPContext *ctx, const char *name) {
- /* TODO(plan 22-03): resolve via current package + export import map. */
- (void)ctx;
+ if (!name || !name[0])
+ return name;
+ /* `__PACKAGE__` resolves to the enclosing package. */
+ if (strcmp(name, "__PACKAGE__") == 0) {
+ if (ctx->enclosing_package_qn && ctx->enclosing_package_qn[0])
+ return ctx->enclosing_package_qn;
+ return ctx->current_package_qn;
+ }
return name;
}
+/* ── @ISA registry helpers ──────────────────────────────────────── */
+
+/* Record `pkg inherits from parent` in the ctx ISA table. Both are package
+ * names (e.g. "Derived", "Base"). */
+static void perl_add_isa(PerlLSPContext *ctx, const char *pkg, const char *parent) {
+ if (!ctx || !pkg || !parent || !pkg[0] || !parent[0])
+ return;
+ if (ctx->isa_count >= ctx->isa_cap) {
+ int newcap = ctx->isa_cap ? ctx->isa_cap * 2 : 8;
+ const char **pk =
+ (const char **)cbm_arena_alloc(ctx->arena, (size_t)newcap * sizeof(char *));
+ const char **pa =
+ (const char **)cbm_arena_alloc(ctx->arena, (size_t)newcap * sizeof(char *));
+ if (!pk || !pa)
+ return;
+ for (int i = 0; i < ctx->isa_count; i++) {
+ pk[i] = ctx->isa_pkg_qns[i];
+ pa[i] = ctx->isa_parent_qns[i];
+ }
+ ctx->isa_pkg_qns = pk;
+ ctx->isa_parent_qns = pa;
+ ctx->isa_cap = newcap;
+ }
+ ctx->isa_pkg_qns[ctx->isa_count] = cbm_arena_strdup(ctx->arena, pkg);
+ ctx->isa_parent_qns[ctx->isa_count] = cbm_arena_strdup(ctx->arena, parent);
+ ctx->isa_count++;
+}
+
+/* ── method lookup over the @ISA chain ──────────────────────────── */
+
+/* Resolve a method on a package, searching the package's own subs first, then
+ * walking parents (@ISA) depth-first. Returns the resolved sub's
+ * CBMRegisteredFunc or NULL. Bounded by CBM_LSP_MAX_LOOKUP_DEPTH * 2 visited.
+ *
+ * package_qn is a package name (e.g. "Foo::Bar"). Methods are matched via the
+ * registered type's method tables (populated in perl_attach_methods) or by a
+ * direct receiver-keyed registry method (stdlib types). */
const CBMRegisteredFunc *perl_lookup_method(PerlLSPContext *ctx, const char *package_qn,
const char *method_name) {
- /* TODO(plan 22-03): walk the @ISA chain in the registry. */
- (void)ctx;
- (void)package_qn;
- (void)method_name;
+ if (!ctx || !package_qn || !method_name)
+ return NULL;
+
+ enum { CAP = CBM_LSP_MAX_LOOKUP_DEPTH * 2 };
+ const char *frontier[CAP];
+ int frontier_count = 0;
+ const char *visited[CAP];
+ int visited_count = 0;
+
+ frontier[frontier_count++] = package_qn;
+
+ while (frontier_count > 0 && visited_count < CAP) {
+ const char *pkg = frontier[--frontier_count];
+ bool seen = false;
+ for (int v = 0; v < visited_count; v++) {
+ if (strcmp(visited[v], pkg) == 0) {
+ seen = true;
+ break;
+ }
+ }
+ if (seen)
+ continue;
+ visited[visited_count++] = pkg;
+
+ const CBMRegisteredType *t = cbm_registry_lookup_type(ctx->registry, pkg);
+ if (!t) {
+ /* Even without a type entry, a stdlib receiver-keyed method may
+ * exist (e.g. a curated CPAN class). */
+ const CBMRegisteredFunc *direct =
+ cbm_registry_lookup_method(ctx->registry, pkg, method_name);
+ if (direct)
+ return direct;
+ continue;
+ }
+
+ /* Own methods (sub table built in perl_attach_methods). */
+ if (t->method_names && t->method_qns) {
+ for (int i = 0; t->method_names[i]; i++) {
+ if (strcmp(t->method_names[i], method_name) == 0) {
+ const CBMRegisteredFunc *f =
+ cbm_registry_lookup_func(ctx->registry, t->method_qns[i]);
+ if (f)
+ return f;
+ }
+ }
+ }
+ /* Direct receiver-keyed method (stdlib types register this way). */
+ const CBMRegisteredFunc *direct =
+ cbm_registry_lookup_method(ctx->registry, pkg, method_name);
+ if (direct)
+ return direct;
+
+ /* Push parents (@ISA) onto the frontier. */
+ if (t->embedded_types) {
+ for (int i = 0; t->embedded_types[i] && frontier_count < CAP; i++)
+ frontier[frontier_count++] = t->embedded_types[i];
+ }
+ }
return NULL;
}
+/* ── expression typing ──────────────────────────────────────────── */
+
+/* Detect a `bless` function call and return the blessed class type, or NULL if
+ * this is not a bless call. Recognizes:
+ * bless($ref, 'Class') → NAMED("Class") (literal)
+ * bless({}, ref($class) || $class) → enclosing package (inferred)
+ * bless $ref, __PACKAGE__ → enclosing package
+ * bless({}) → enclosing package (1-arg form) */
+static const CBMType *perl_eval_bless(PerlLSPContext *ctx, TSNode call_node) {
+ const char *k = ts_node_type(call_node);
+ if (strcmp(k, "function_call_expression") != 0 &&
+ strcmp(k, "ambiguous_function_call_expression") != 0)
+ return NULL;
+
+ TSNode fn = ts_node_child_by_field_name(call_node, "function", 8);
+ if (ts_node_is_null(fn))
+ return NULL;
+ char *fname = perl_node_text(ctx, fn);
+ if (!fname || strcmp(fname, "bless") != 0)
+ return NULL;
+
+ TSNode args = ts_node_child_by_field_name(call_node, "arguments", 9);
+ if (ts_node_is_null(args))
+ args = call_node; /* arguments may be inline children */
+
+ /* Find the SECOND meaningful argument (the class). The first is the ref. */
+ int seen = 0;
+ TSNode class_arg;
+ memset(&class_arg, 0, sizeof(class_arg));
+ bool have_class = false;
+ uint32_t nc = ts_node_child_count(args);
+ for (uint32_t i = 0; i < nc; i++) {
+ TSNode c = ts_node_child(args, i);
+ if (ts_node_is_null(c) || !ts_node_is_named(c))
+ continue;
+ const char *ck = ts_node_type(c);
+ /* Skip the literal "bless" callee if args==call_node. */
+ if (strcmp(ck, "function") == 0)
+ continue;
+ seen++;
+ if (seen == 2) {
+ class_arg = c;
+ have_class = true;
+ break;
+ }
+ }
+
+ const char *pkg =
+ ctx->enclosing_package_qn ? ctx->enclosing_package_qn : ctx->current_package_qn;
+
+ if (!have_class) {
+ /* 1-arg bless: blesses into the current package. */
+ if (pkg && pkg[0])
+ return cbm_type_named(ctx->arena, pkg);
+ return cbm_type_unknown();
+ }
+
+ const char *ack = ts_node_type(class_arg);
+ if (perl_is_string_node(ack)) {
+ char *raw = perl_node_text(ctx, class_arg);
+ char *inner = perl_unquote(ctx->arena, raw);
+ if (inner && inner[0])
+ return cbm_type_named(ctx->arena, perl_resolve_package_name(ctx, inner));
+ } else if (perl_is_bareword_node(ack)) {
+ char *bw = perl_node_text(ctx, class_arg);
+ if (bw && strcmp(bw, "__PACKAGE__") == 0) {
+ if (pkg && pkg[0])
+ return cbm_type_named(ctx->arena, pkg);
+ } else if (bw && bw[0]) {
+ return cbm_type_named(ctx->arena, perl_resolve_package_name(ctx, bw));
+ }
+ } else {
+ /* ref($class) || $class / $class → the enclosing sub's invocant
+ * class. Bind to the enclosing package as the best static guess
+ * (standard constructor idiom). */
+ if (pkg && pkg[0])
+ return cbm_type_named(ctx->arena, pkg);
+ }
+ return cbm_type_unknown();
+}
+
+const CBMType *perl_eval_expr_type(PerlLSPContext *ctx, TSNode node) {
+ if (ts_node_is_null(node))
+ return cbm_type_unknown();
+
+ /* Recursion guard (mirrors php_eval_expr_type, cap PERL_EVAL_MAX_DEPTH). */
+ if (ctx->eval_depth >= PERL_EVAL_MAX_DEPTH)
+ return cbm_type_unknown();
+ ctx->eval_depth++;
+ const CBMType *result = cbm_type_unknown();
+
+ const char *k = ts_node_type(node);
+
+ if (strcmp(k, "scalar") == 0 || strcmp(k, "scalar_variable") == 0) {
+ char *txt = perl_node_text(ctx, node);
+ if (txt) {
+ const char *bare = perl_strip_sigil(txt);
+ const CBMType *t = cbm_scope_lookup(ctx->current_scope, bare);
+ if (t)
+ result = t;
+ }
+ } else if (strcmp(k, "method_call_expression") == 0) {
+ result = perl_eval_method_call_type(ctx, node);
+ } else if (strcmp(k, "function_call_expression") == 0 ||
+ strcmp(k, "ambiguous_function_call_expression") == 0) {
+ const CBMType *blessed = perl_eval_bless(ctx, node);
+ if (blessed && !cbm_type_is_unknown(blessed))
+ result = blessed;
+ else
+ result = perl_eval_function_call_type(ctx, node);
+ } else if (strcmp(k, "assignment_expression") == 0) {
+ TSNode right = ts_node_child_by_field_name(node, "right", 5);
+ if (!ts_node_is_null(right))
+ result = perl_eval_expr_type(ctx, right);
+ } else if (strcmp(k, "variable_declaration") == 0) {
+ /* `my $x = EXPR;` — the `=` is wrapped in an assignment_expression
+ * child; recurse into it. */
+ TSNode assign = perl_first_child_of_type(node, "assignment_expression");
+ if (!ts_node_is_null(assign))
+ result = perl_eval_expr_type(ctx, assign);
+ } else if (strcmp(k, "parenthesized_expression") == 0 || strcmp(k, "list_expression") == 0) {
+ /* Unwrap a single meaningful child. */
+ uint32_t nc = ts_node_child_count(node);
+ for (uint32_t i = 0; i < nc; i++) {
+ TSNode c = ts_node_child(node, i);
+ if (ts_node_is_null(c) || !ts_node_is_named(c))
+ continue;
+ result = perl_eval_expr_type(ctx, c);
+ break;
+ }
+ }
+ /* Hash/array deref of an unknown type → unknown (no edge). Anything we did
+ * not recognize stays unknown. */
+
+ ctx->eval_depth--;
+ return result;
+}
+
+/* ClassName->new(...) returns ClassName. Handles the method_call_expression
+ * where the invocant is a bareword/string class and the method is `new`.
+ * Returns the constructed type, or NULL if this is not a constructor call. */
+static const CBMType *perl_eval_new_type(PerlLSPContext *ctx, TSNode node) {
+ TSNode inv = ts_node_child_by_field_name(node, "invocant", 8);
+ TSNode meth = ts_node_child_by_field_name(node, "method", 6);
+ if (ts_node_is_null(inv) || ts_node_is_null(meth))
+ return NULL;
+ char *mname = perl_node_text(ctx, meth);
+ if (!mname || strcmp(mname, "new") != 0)
+ return NULL;
+ const char *ik = ts_node_type(inv);
+ if (perl_is_bareword_node(ik)) {
+ char *cls = perl_node_text(ctx, inv);
+ if (cls && cls[0])
+ return cbm_type_named(ctx->arena, perl_resolve_package_name(ctx, cls));
+ } else if (perl_is_string_node(ik)) {
+ char *raw = perl_node_text(ctx, inv);
+ char *inner = perl_unquote(ctx->arena, raw);
+ if (inner && inner[0])
+ return cbm_type_named(ctx->arena, perl_resolve_package_name(ctx, inner));
+ }
+ return NULL;
+}
+
+/* func() in the current package, or Package::func() static call. Returns the
+ * function's return type (for chaining), or unknown. */
+static const CBMType *perl_eval_function_call_type(PerlLSPContext *ctx, TSNode node) {
+ TSNode fn = ts_node_child_by_field_name(node, "function", 8);
+ if (ts_node_is_null(fn))
+ return cbm_type_unknown();
+ char *name = perl_node_text(ctx, fn);
+ if (!name || !name[0])
+ return cbm_type_unknown();
+
+ const CBMRegisteredFunc *f = NULL;
+
+ /* Package::func() — qualified static call. */
+ char *colons = strstr(name, "::");
+ if (colons) {
+ size_t plen = (size_t)(colons - name);
+ char *pkg = cbm_arena_strndup(ctx->arena, name, plen);
+ const char *shortn = colons + 2;
+ f = perl_lookup_method(ctx, pkg, shortn);
+ if (!f)
+ f = cbm_registry_lookup_symbol(ctx->registry, pkg, shortn);
+ } else {
+ /* Bare func() — Exporter import map, then file-local/global func. */
+ const char *imp = perl_find_import(ctx, name);
+ if (imp)
+ f = cbm_registry_lookup_func(ctx->registry, imp);
+ if (!f)
+ f = cbm_registry_lookup_symbol(ctx->registry, ctx->module_qn, name);
+ }
+ if (f && f->signature && f->signature->kind == CBM_TYPE_FUNC &&
+ f->signature->data.func.return_types && f->signature->data.func.return_types[0]) {
+ return f->signature->data.func.return_types[0];
+ }
+ return cbm_type_unknown();
+}
+
+/* $obj->m / Class->m / $self->m — returns the method's return type. */
+static const CBMType *perl_eval_method_call_type(PerlLSPContext *ctx, TSNode node) {
+ /* ClassName->new returns ClassName (constructor). */
+ const CBMType *ctor = perl_eval_new_type(ctx, node);
+ if (ctor)
+ return ctor;
+
+ TSNode inv = ts_node_child_by_field_name(node, "invocant", 8);
+ TSNode meth = ts_node_child_by_field_name(node, "method", 6);
+ if (ts_node_is_null(meth))
+ return cbm_type_unknown();
+ char *mname = perl_node_text(ctx, meth);
+ if (!mname || !mname[0])
+ return cbm_type_unknown();
+
+ const char *class_qn = NULL;
+ if (!ts_node_is_null(inv)) {
+ const char *ik = ts_node_type(inv);
+ if (perl_is_bareword_node(ik)) {
+ char *cls = perl_node_text(ctx, inv);
+ if (cls && cls[0])
+ class_qn = perl_resolve_package_name(ctx, cls);
+ } else {
+ const CBMType *recv = perl_eval_expr_type(ctx, inv);
+ if (recv && recv->kind == CBM_TYPE_NAMED)
+ class_qn = recv->data.named.qualified_name;
+ }
+ }
+ if (!class_qn)
+ return cbm_type_unknown();
+
+ const CBMRegisteredFunc *f = perl_lookup_method(ctx, class_qn, mname);
+ if (f && f->signature && f->signature->kind == CBM_TYPE_FUNC &&
+ f->signature->data.func.return_types && f->signature->data.func.return_types[0]) {
+ return f->signature->data.func.return_types[0];
+ }
+ return cbm_type_unknown();
+}
+
+/* ── emit ───────────────────────────────────────────────────────── */
+
+static void perl_emit_resolved(PerlLSPContext *ctx, const char *callee_qn, const char *strategy,
+ float confidence) {
+ if (!ctx->resolved_calls || !callee_qn || !ctx->enclosing_func_qn)
+ return;
+ CBMResolvedCall rc;
+ rc.caller_qn = ctx->enclosing_func_qn;
+ rc.callee_qn = callee_qn;
+ rc.strategy = strategy;
+ rc.confidence = confidence;
+ rc.reason = NULL;
+ cbm_resolvedcall_push(ctx->resolved_calls, ctx->arena, rc);
+}
+
+/* ── call/method dispatch (emit edges) ──────────────────────────── */
+
+/* Resolve a function/static call and emit an edge if it lands on a registered
+ * sub. Bare func(), Exporter func(), and Package::func() static calls. */
+static void perl_resolve_function_call(PerlLSPContext *ctx, TSNode call) {
+ TSNode fn = ts_node_child_by_field_name(call, "function", 8);
+ if (ts_node_is_null(fn))
+ return;
+ char *name = perl_node_text(ctx, fn);
+ if (!name || !name[0])
+ return;
+ /* `bless` is a typing primitive, not a resolvable user call. */
+ if (strcmp(name, "bless") == 0)
+ return;
+
+ const CBMRegisteredFunc *f = NULL;
+ char *colons = strstr(name, "::");
+ if (colons) {
+ size_t plen = (size_t)(colons - name);
+ char *pkg = cbm_arena_strndup(ctx->arena, name, plen);
+ const char *shortn = colons + 2;
+ f = perl_lookup_method(ctx, pkg, shortn);
+ if (!f)
+ f = cbm_registry_lookup_symbol(ctx->registry, pkg, shortn);
+ if (f) {
+ perl_emit_resolved(ctx, f->qualified_name, "perl_static_call", PERL_CONF_LITERAL);
+ return;
+ }
+ } else {
+ const char *imp = perl_find_import(ctx, name);
+ if (imp) {
+ f = cbm_registry_lookup_func(ctx->registry, imp);
+ if (f) {
+ perl_emit_resolved(ctx, f->qualified_name, "perl_imported_function",
+ PERL_CONF_LITERAL);
+ return;
+ }
+ }
+ f = cbm_registry_lookup_symbol(ctx->registry, ctx->module_qn, name);
+ if (f) {
+ perl_emit_resolved(ctx, f->qualified_name, "perl_function_local", PERL_CONF_LITERAL);
+ return;
+ }
+ }
+ /* Unresolved — emit nothing (the unified extractor already records the raw
+ * call edge; zero spurious edges). */
+}
+
+/* Resolve a method call and emit an edge if the receiver type is known AND the
+ * method resolves through the @ISA chain. Unknown receiver → NO edge. */
+static void perl_resolve_method_call(PerlLSPContext *ctx, TSNode call) {
+ /* Class->new constructor: only meaningful for typing, not a callable user
+ * sub unless the package actually defines new — fall through to lookup. */
+ TSNode inv = ts_node_child_by_field_name(call, "invocant", 8);
+ TSNode meth = ts_node_child_by_field_name(call, "method", 6);
+ if (ts_node_is_null(meth))
+ return;
+ char *mname = perl_node_text(ctx, meth);
+ if (!mname || !mname[0])
+ return;
+
+ const char *class_qn = NULL;
+ const char *strategy = "perl_method_typed";
+ if (!ts_node_is_null(inv)) {
+ const char *ik = ts_node_type(inv);
+ if (perl_is_bareword_node(ik)) {
+ char *cls = perl_node_text(ctx, inv);
+ if (cls && cls[0])
+ class_qn = perl_resolve_package_name(ctx, cls);
+ strategy = "perl_method_static";
+ } else {
+ const CBMType *recv = perl_eval_expr_type(ctx, inv);
+ if (recv && recv->kind == CBM_TYPE_NAMED) {
+ class_qn = recv->data.named.qualified_name;
+ strategy = "perl_method_typed";
+ }
+ }
+ }
+ if (!class_qn)
+ return; /* unknown receiver — zero-edge guarantee */
+
+ const CBMRegisteredFunc *f = perl_lookup_method(ctx, class_qn, mname);
+ if (f) {
+ const char *strat = (f->receiver_type && strcmp(f->receiver_type, class_qn) == 0)
+ ? strategy
+ : "perl_method_inherited";
+ perl_emit_resolved(ctx, f->qualified_name, strat, PERL_CONF_LITERAL);
+ return;
+ }
+ /* Receiver typed but method not found in the indexed inheritance chain.
+ * Per the zero-edge guarantee, emit nothing rather than a guessed edge. */
+}
+
+/* ── assignment observer (scope binding) ────────────────────────── */
+
+/* Bind an LHS scalar to the RHS type. Handles `my $x = EXPR;` and `$x = EXPR;`.
+ * Only single scalar targets are tracked (list assignment is skipped). */
+static void perl_process_assignment(PerlLSPContext *ctx, TSNode assign) {
+ TSNode left = ts_node_child_by_field_name(assign, "left", 4);
+ TSNode right = ts_node_child_by_field_name(assign, "right", 5);
+ if (ts_node_is_null(left) || ts_node_is_null(right))
+ return;
+
+ TSNode lhs_var = perl_decl_target(left);
+ const char *lvk = ts_node_type(lhs_var);
+ if (strcmp(lvk, "scalar") != 0 && strcmp(lvk, "scalar_variable") != 0)
+ return;
+
+ char *vtxt = perl_node_text(ctx, lhs_var);
+ if (!vtxt)
+ return;
+ const char *bare = perl_strip_sigil(vtxt);
+ if (!bare || !bare[0])
+ return;
+
+ const CBMType *rt = perl_eval_expr_type(ctx, right);
+ if (rt && rt->kind == CBM_TYPE_NAMED)
+ cbm_scope_bind(ctx->current_scope, bare, rt);
+}
+
+/* ── body walk ──────────────────────────────────────────────────── */
+
+static void perl_resolve_calls_in_node(PerlLSPContext *ctx, TSNode node) {
+ if (ts_node_is_null(node))
+ return;
+ const char *k = ts_node_type(node);
+
+ /* Nested subs get their own scope via process_subroutine. */
+ if (strcmp(k, "subroutine_declaration_statement") == 0 ||
+ strcmp(k, "method_declaration_statement") == 0 ||
+ strcmp(k, "anonymous_subroutine_expression") == 0) {
+ process_subroutine(ctx, node);
+ return;
+ }
+ /* A block-scoped package: `package Foo { ... }` updates package context. */
+ if (strcmp(k, "package_statement") == 0) {
+ process_package_decl(ctx, node);
+ /* Continue walking children (block body may follow). */
+ }
+
+ /* Scope-binding observers. `my $x = bless(...)` is a variable_declaration
+ * wrapping an assignment_expression; handle both forms. */
+ if (strcmp(k, "assignment_expression") == 0) {
+ perl_process_assignment(ctx, node);
+ } else if (strcmp(k, "variable_declaration") == 0) {
+ TSNode assign = perl_first_child_of_type(node, "assignment_expression");
+ if (!ts_node_is_null(assign))
+ perl_process_assignment(ctx, assign);
+ }
+
+ /* Call-resolution dispatch. */
+ if (strcmp(k, "function_call_expression") == 0 ||
+ strcmp(k, "ambiguous_function_call_expression") == 0) {
+ perl_resolve_function_call(ctx, node);
+ } else if (strcmp(k, "method_call_expression") == 0) {
+ perl_resolve_method_call(ctx, node);
+ }
+
+ /* Recurse. */
+ uint32_t nc = ts_node_child_count(node);
+ for (uint32_t i = 0; i < nc; i++) {
+ TSNode c = ts_node_child(node, i);
+ if (!ts_node_is_null(c))
+ perl_resolve_calls_in_node(ctx, c);
+ }
+}
+
+/* ── subroutine processing ──────────────────────────────────────── */
+
+/* Find the sub's name via the `name` field. */
+static char *perl_sub_name(PerlLSPContext *ctx, TSNode node) {
+ TSNode name = ts_node_child_by_field_name(node, "name", 4);
+ if (ts_node_is_null(name))
+ return NULL;
+ return perl_node_text(ctx, name);
+}
+
+/* Bind the invocant: in a method sub belonging to package P, the first
+ * statement is typically `my $self = shift;` or `my $class = shift;`. Bind the
+ * first such scalar to type P so $self->method() / $class->method() dispatch. */
+static void perl_infer_self_type(PerlLSPContext *ctx, TSNode body) {
+ const char *pkg =
+ ctx->enclosing_package_qn ? ctx->enclosing_package_qn : ctx->current_package_qn;
+ if (!pkg || !pkg[0])
+ return;
+ uint32_t nc = ts_node_child_count(body);
+ for (uint32_t i = 0; i < nc; i++) {
+ TSNode stmt = ts_node_child(body, i);
+ if (ts_node_is_null(stmt) || !ts_node_is_named(stmt))
+ continue;
+
+ TSNode assign;
+ memset(&assign, 0, sizeof(assign));
+ const char *sk = ts_node_type(stmt);
+ if (strcmp(sk, "expression_statement") == 0) {
+ TSNode a = perl_first_child_of_type(stmt, "assignment_expression");
+ if (!ts_node_is_null(a)) {
+ assign = a;
+ } else {
+ TSNode vd = perl_first_child_of_type(stmt, "variable_declaration");
+ if (!ts_node_is_null(vd))
+ assign = perl_first_child_of_type(vd, "assignment_expression");
+ }
+ } else if (strcmp(sk, "variable_declaration") == 0) {
+ assign = perl_first_child_of_type(stmt, "assignment_expression");
+ } else if (strcmp(sk, "assignment_expression") == 0) {
+ assign = stmt;
+ }
+ if (ts_node_is_null(assign))
+ continue;
+
+ TSNode left = ts_node_child_by_field_name(assign, "left", 4);
+ TSNode right = ts_node_child_by_field_name(assign, "right", 5);
+ if (ts_node_is_null(left) || ts_node_is_null(right))
+ continue;
+ TSNode lhs_var = perl_decl_target(left);
+ const char *lvk = ts_node_type(lhs_var);
+ if (strcmp(lvk, "scalar") != 0 && strcmp(lvk, "scalar_variable") != 0)
+ continue;
+
+ /* RHS must reference `shift` (the invocant idiom). */
+ char *rtxt = perl_node_text(ctx, right);
+ if (!rtxt || !strstr(rtxt, "shift"))
+ continue;
+
+ char *vtxt = perl_node_text(ctx, lhs_var);
+ if (!vtxt)
+ continue;
+ const char *bare = perl_strip_sigil(vtxt);
+ if (bare && bare[0])
+ cbm_scope_bind(ctx->current_scope, bare, cbm_type_named(ctx->arena, pkg));
+ return; /* only the first invocant binding */
+ }
+}
+
+static void process_subroutine(PerlLSPContext *ctx, TSNode node) {
+ CBMScope *saved_scope = ctx->current_scope;
+ const char *saved_func = ctx->enclosing_func_qn;
+
+ ctx->current_scope = cbm_scope_push(ctx->arena, ctx->current_scope);
+
+ /* Sub QN = module_qn.subname (package is NOT woven in — see file header). */
+ char *sname = perl_sub_name(ctx, node);
+ if (sname && sname[0]) {
+ if (ctx->module_qn)
+ ctx->enclosing_func_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->module_qn, sname);
+ else
+ ctx->enclosing_func_qn = cbm_arena_strdup(ctx->arena, sname);
+ }
+
+ /* Locate the body block. */
+ TSNode body = ts_node_child_by_field_name(node, "body", 4);
+ if (ts_node_is_null(body))
+ body = perl_first_child_of_type(node, "block");
+
+ if (!ts_node_is_null(body)) {
+ perl_infer_self_type(ctx, body);
+ perl_resolve_calls_in_node(ctx, body);
+ }
+
+ ctx->current_scope = saved_scope;
+ ctx->enclosing_func_qn = saved_func;
+}
+
+/* ── package + use collection (PASS 1) ──────────────────────────── */
+
+/* Set the current package from a package_statement. */
+static void process_package_decl(PerlLSPContext *ctx, TSNode node) {
+ TSNode name = ts_node_child_by_field_name(node, "name", 4);
+ if (ts_node_is_null(name))
+ name = perl_first_child_of_type(node, "package");
+ if (ts_node_is_null(name))
+ return;
+ char *pkg = perl_node_text(ctx, name);
+ if (!pkg || !pkg[0])
+ return;
+ ctx->current_package_qn = cbm_arena_strdup(ctx->arena, pkg);
+ ctx->enclosing_package_qn = ctx->current_package_qn;
+}
+
+/* Parse the `qw(a b c)` list inside a node into the import map for module
+ * `module_name`: each word W maps to `module_name::W`. */
+static void perl_collect_qw_imports(PerlLSPContext *ctx, TSNode container,
+ const char *module_name) {
+ TSNode qw = perl_first_child_of_type(container, "quoted_word_list");
+ if (ts_node_is_null(qw))
+ return;
+ uint32_t nc = ts_node_child_count(qw);
+ for (uint32_t i = 0; i < nc; i++) {
+ TSNode w = ts_node_child(qw, i);
+ if (ts_node_is_null(w) || !ts_node_is_named(w))
+ continue;
+ char *word = perl_node_text(ctx, w);
+ if (!word || !word[0])
+ continue;
+ const char *fn = perl_strip_sigil(word); /* allow &func imports */
+ if (!fn || !fn[0] || !(isalpha((unsigned char)fn[0]) || fn[0] == '_'))
+ continue;
+ char *target = cbm_arena_sprintf(ctx->arena, "%s::%s", module_name, fn);
+ perl_lsp_add_use(ctx, fn, target);
+ }
+}
+
+/* Recursively collect parent package names from a subtree, registering each
+ * as an @ISA parent of `child_pkg`. Accepts string literals, barewords, and
+ * `quoted_word_list` words, descending through `list_expression` /
+ * parenthesized wrappers. Skips the `-norequire` flag and the leading
+ * `parent`/`base` module barewords. Bounded recursion depth. */
+static void perl_collect_parents(PerlLSPContext *ctx, TSNode node, const char *child_pkg,
+ int depth) {
+ if (ts_node_is_null(node) || depth > 6)
+ return;
+ const char *k = ts_node_type(node);
+ if (perl_is_string_node(k)) {
+ char *raw = perl_node_text(ctx, node);
+ char *inner = perl_unquote(ctx->arena, raw);
+ if (inner && inner[0] && strcmp(inner, "-norequire") != 0)
+ perl_add_isa(ctx, child_pkg, inner);
+ return;
+ }
+ if (perl_is_bareword_node(k)) {
+ char *bw = perl_node_text(ctx, node);
+ if (bw && bw[0] && strcmp(bw, "parent") != 0 && strcmp(bw, "base") != 0 &&
+ strcmp(bw, "-norequire") != 0 && bw[0] != '-')
+ perl_add_isa(ctx, child_pkg, bw);
+ return;
+ }
+ /* quoted_word_list words come through as named string-content children. */
+ if (strcmp(k, "quoted_word_list") == 0) {
+ uint32_t nc = ts_node_child_count(node);
+ for (uint32_t i = 0; i < nc; i++) {
+ TSNode w = ts_node_child(node, i);
+ if (ts_node_is_null(w) || !ts_node_is_named(w))
+ continue;
+ char *pw = perl_node_text(ctx, w);
+ if (pw && pw[0] && strcmp(pw, "-norequire") == 0)
+ continue;
+ if (pw && pw[0])
+ perl_add_isa(ctx, child_pkg, pw);
+ }
+ return;
+ }
+ /* list_expression / parenthesized: descend. */
+ uint32_t nc = ts_node_child_count(node);
+ for (uint32_t i = 0; i < nc; i++) {
+ TSNode c = ts_node_child(node, i);
+ if (!ts_node_is_null(c) && ts_node_is_named(c))
+ perl_collect_parents(ctx, c, child_pkg, depth + 1);
+ }
+}
+
+/* Process a `use_statement`:
+ * use parent qw(Base); / use parent 'Base'; → @ISA for current package
+ * use base qw(Base); / use base -norequire => 'Base';
+ * use Module qw(f1 f2); → Exporter import map (f1→Module::f1) */
+static void perl_collect_use_statement(PerlLSPContext *ctx, TSNode node) {
+ TSNode mod = ts_node_child_by_field_name(node, "module", 6);
+ char *module_name = NULL;
+ if (!ts_node_is_null(mod))
+ module_name = perl_node_text(ctx, mod);
+ if (!module_name || !module_name[0])
+ return;
+
+ bool is_parent = strcmp(module_name, "parent") == 0;
+ bool is_base = strcmp(module_name, "base") == 0;
+
+ if (is_parent || is_base) {
+ const char *child_pkg = ctx->current_package_qn && ctx->current_package_qn[0]
+ ? ctx->current_package_qn
+ : "main";
+ /* Parent package names appear as `use_statement` arguments — directly,
+ * inside a `list_expression` (use parent -norequire, 'Base'), or in a
+ * `quoted_word_list` (use parent qw(Base)). Scan every named child
+ * except the leading `module` bareword (parent/base). */
+ uint32_t nc = ts_node_child_count(node);
+ for (uint32_t i = 0; i < nc; i++) {
+ TSNode c = ts_node_child(node, i);
+ if (ts_node_is_null(c) || !ts_node_is_named(c))
+ continue;
+ /* Skip the module bareword itself (it equals "parent"/"base"). */
+ if (ts_node_eq(c, mod))
+ continue;
+ perl_collect_parents(ctx, c, child_pkg, 0);
+ }
+ return;
+ }
+
+ /* Generic Exporter import: use Module qw(f1 f2). */
+ perl_collect_qw_imports(ctx, node, module_name);
+}
+
+/* Detect `our @ISA = (...)` / `@ISA = (...)` assignments, recording parents
+ * for the current package. */
+static void perl_collect_isa_assignment(PerlLSPContext *ctx, TSNode assign) {
+ TSNode left = ts_node_child_by_field_name(assign, "left", 4);
+ if (ts_node_is_null(left))
+ return;
+ TSNode lhs = perl_decl_target(left);
+ char *ltxt = perl_node_text(ctx, lhs);
+ if (!ltxt)
+ return;
+ const char *bare = perl_strip_sigil(ltxt);
+ /* Match @ISA (bare) and qualified Pkg::ISA forms. */
+ if (!bare)
+ return;
+ const char *tail = strstr(bare, "ISA");
+ bool is_isa = (strcmp(bare, "ISA") == 0) ||
+ (tail && strcmp(tail, "ISA") == 0 && tail > bare && *(tail - 1) == ':');
+ if (!is_isa)
+ return;
+
+ const char *child_pkg =
+ ctx->current_package_qn && ctx->current_package_qn[0] ? ctx->current_package_qn : "main";
+
+ TSNode right = ts_node_child_by_field_name(assign, "right", 5);
+ if (ts_node_is_null(right))
+ return;
+ /* Parents may be a quoted_word_list, a list_expression of string literals,
+ * or a bare string literal. The recursive collector handles all forms. */
+ perl_collect_parents(ctx, right, child_pkg, 0);
+}
+
+/* Recursively scan (PASS 1) for package context, @ISA assignments, and `use`
+ * statements. */
+static void perl_pass1_scan(PerlLSPContext *ctx, TSNode node) {
+ if (ts_node_is_null(node))
+ return;
+ const char *k = ts_node_type(node);
+ if (strcmp(k, "package_statement") == 0) {
+ process_package_decl(ctx, node);
+ /* Fall through: a block-scoped package's body follows as children. */
+ } else if (strcmp(k, "use_statement") == 0) {
+ perl_collect_use_statement(ctx, node);
+ return;
+ } else if (strcmp(k, "assignment_expression") == 0) {
+ perl_collect_isa_assignment(ctx, node);
+ }
+ uint32_t nc = ts_node_child_count(node);
+ for (uint32_t i = 0; i < nc; i++) {
+ TSNode c = ts_node_child(node, i);
+ if (!ts_node_is_null(c))
+ perl_pass1_scan(ctx, c);
+ }
+}
+
+/* ── process_file: two-pass walk ────────────────────────────────── */
+
+void perl_lsp_process_file(PerlLSPContext *ctx, TSNode root) {
+ if (ts_node_is_null(root))
+ return;
+
+ /* PASS 1: collect package context, @ISA inheritance, Exporter imports.
+ * Reset the per-file maps first so this is idempotent even when a caller
+ * (cbm_run_perl_lsp) has already run a pre-pass to build registry types. */
+ ctx->current_package_qn = "";
+ ctx->enclosing_package_qn = "";
+ ctx->use_count = 0;
+ ctx->isa_count = 0;
+ perl_pass1_scan(ctx, root);
+
+ /* PASS 2: walk subs in package order; resolve + emit call edges. */
+ ctx->current_package_qn = "";
+ ctx->enclosing_package_qn = "";
+ uint32_t nc = ts_node_child_count(root);
+ for (uint32_t i = 0; i < nc; i++) {
+ TSNode c = ts_node_child(root, i);
+ if (ts_node_is_null(c))
+ continue;
+ const char *k = ts_node_type(c);
+ if (strcmp(k, "package_statement") == 0) {
+ process_package_decl(ctx, c);
+ /* Walk the (possibly block-scoped) package body for nested subs. */
+ uint32_t bn = ts_node_child_count(c);
+ for (uint32_t bi = 0; bi < bn; bi++) {
+ TSNode bc = ts_node_child(c, bi);
+ if (!ts_node_is_null(bc) && ts_node_is_named(bc))
+ perl_resolve_calls_in_node(ctx, bc);
+ }
+ } else if (strcmp(k, "subroutine_declaration_statement") == 0 ||
+ strcmp(k, "method_declaration_statement") == 0) {
+ process_subroutine(ctx, c);
+ } else {
+ /* Top-level statements: walk for nested subs / block packages.
+ * Edges outside an enclosing sub are suppressed (no caller QN). */
+ perl_resolve_calls_in_node(ctx, c);
+ }
+ }
+}
+
+/* ── registry: per-package types + method tables ────────────────── */
+
+/* Register a per-package CBMRegisteredType for every package that participates
+ * in @ISA (as child or parent), then attach @ISA parents (embedded_types). */
+static void perl_register_packages(PerlLSPContext *ctx, CBMTypeRegistry *reg) {
+ for (int i = 0; i < ctx->isa_count; i++) {
+ const char *names[2] = {ctx->isa_pkg_qns[i], ctx->isa_parent_qns[i]};
+ for (int s = 0; s < 2; s++) {
+ const char *pkg = names[s];
+ if (!pkg || !pkg[0] || cbm_registry_lookup_type(reg, pkg))
+ continue;
+ CBMRegisteredType rt;
+ memset(&rt, 0, sizeof(rt));
+ rt.qualified_name = cbm_arena_strdup(ctx->arena, pkg);
+ rt.short_name = rt.qualified_name;
+ cbm_registry_add_type(reg, rt);
+ }
+ }
+
+ /* Attach @ISA parents (embedded_types) to each child package type. */
+ for (int t = 0; t < reg->type_count; t++) {
+ CBMRegisteredType *rt = &reg->types[t];
+ if (!rt->qualified_name)
+ continue;
+ int pc = 0;
+ for (int i = 0; i < ctx->isa_count; i++) {
+ if (strcmp(ctx->isa_pkg_qns[i], rt->qualified_name) == 0)
+ pc++;
+ }
+ if (pc == 0)
+ continue;
+ const char **parents =
+ (const char **)cbm_arena_alloc(ctx->arena, (size_t)(pc + 1) * sizeof(char *));
+ if (!parents)
+ continue;
+ int w = 0;
+ for (int i = 0; i < ctx->isa_count; i++) {
+ if (strcmp(ctx->isa_pkg_qns[i], rt->qualified_name) == 0)
+ parents[w++] = ctx->isa_parent_qns[i];
+ }
+ parents[w] = NULL;
+ rt->embedded_types = parents;
+ }
+}
+
+/* Append a (short_name → sub_qn) entry to the package type's method tables,
+ * creating the type if needed. */
+static void perl_type_add_method(PerlLSPContext *ctx, CBMTypeRegistry *reg, const char *pkg,
+ const char *short_name, const char *sub_qn) {
+ if (!cbm_registry_lookup_type(reg, pkg)) {
+ CBMRegisteredType rt;
+ memset(&rt, 0, sizeof(rt));
+ rt.qualified_name = cbm_arena_strdup(ctx->arena, pkg);
+ rt.short_name = rt.qualified_name;
+ cbm_registry_add_type(reg, rt);
+ }
+ for (int t = 0; t < reg->type_count; t++) {
+ CBMRegisteredType *rt = &reg->types[t];
+ if (!rt->qualified_name || strcmp(rt->qualified_name, pkg) != 0)
+ continue;
+ int cnt = 0;
+ if (rt->method_names)
+ while (rt->method_names[cnt])
+ cnt++;
+ const char **mn =
+ (const char **)cbm_arena_alloc(ctx->arena, (size_t)(cnt + 2) * sizeof(char *));
+ const char **mq =
+ (const char **)cbm_arena_alloc(ctx->arena, (size_t)(cnt + 2) * sizeof(char *));
+ if (!mn || !mq)
+ return;
+ for (int j = 0; j < cnt; j++) {
+ mn[j] = rt->method_names[j];
+ mq[j] = rt->method_qns[j];
+ }
+ mn[cnt] = cbm_arena_strdup(ctx->arena, short_name);
+ mq[cnt] = sub_qn;
+ mn[cnt + 1] = NULL;
+ mq[cnt + 1] = NULL;
+ rt->method_names = mn;
+ rt->method_qns = mq;
+ return;
+ }
+}
+
+/* Walk the top level mapping each sub to its enclosing package, registering the
+ * sub's QN in that package's method table so method dispatch finds it. */
+static void perl_attach_methods(PerlLSPContext *ctx, CBMTypeRegistry *reg, TSNode root) {
+ const char *cur_pkg = "main";
+ uint32_t nc = ts_node_child_count(root);
+ for (uint32_t i = 0; i < nc; i++) {
+ TSNode c = ts_node_child(root, i);
+ if (ts_node_is_null(c))
+ continue;
+ const char *k = ts_node_type(c);
+ if (strcmp(k, "package_statement") == 0) {
+ TSNode name = ts_node_child_by_field_name(c, "name", 4);
+ if (ts_node_is_null(name))
+ name = perl_first_child_of_type(c, "package");
+ if (!ts_node_is_null(name)) {
+ char *p = perl_node_text(ctx, name);
+ if (p && p[0])
+ cur_pkg = cbm_arena_strdup(ctx->arena, p);
+ }
+ /* Block-scoped package body: subs are nested children. */
+ uint32_t bn = ts_node_child_count(c);
+ for (uint32_t bi = 0; bi < bn; bi++) {
+ TSNode bc = ts_node_child(c, bi);
+ if (ts_node_is_null(bc) || !ts_node_is_named(bc))
+ continue;
+ if (strcmp(ts_node_type(bc), "subroutine_declaration_statement") != 0 &&
+ strcmp(ts_node_type(bc), "method_declaration_statement") != 0)
+ continue;
+ TSNode bname = ts_node_child_by_field_name(bc, "name", 4);
+ if (ts_node_is_null(bname))
+ continue;
+ char *bsn = perl_node_text(ctx, bname);
+ if (!bsn || !bsn[0])
+ continue;
+ const char *bqn = ctx->module_qn
+ ? cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->module_qn, bsn)
+ : cbm_arena_strdup(ctx->arena, bsn);
+ perl_type_add_method(ctx, reg, cur_pkg, bsn, bqn);
+ }
+ continue;
+ }
+ if (strcmp(k, "subroutine_declaration_statement") != 0 &&
+ strcmp(k, "method_declaration_statement") != 0)
+ continue;
+
+ TSNode name = ts_node_child_by_field_name(c, "name", 4);
+ if (ts_node_is_null(name))
+ continue;
+ char *sname = perl_node_text(ctx, name);
+ if (!sname || !sname[0])
+ continue;
+ const char *sub_qn = ctx->module_qn
+ ? cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->module_qn, sname)
+ : cbm_arena_strdup(ctx->arena, sname);
+ perl_type_add_method(ctx, reg, cur_pkg, sname, sub_qn);
+ }
+}
+
/* ── entry: cbm_run_perl_lsp ────────────────────────────────────── */
void cbm_run_perl_lsp(CBMArena *arena, CBMFileResult *result, const char *source, int source_len,
@@ -121,26 +1223,25 @@ void cbm_run_perl_lsp(CBMArena *arena, CBMFileResult *result, const char *source
CBMTypeRegistry reg;
cbm_registry_init(&reg, arena);
- /* Phase A: register stdlib types/functions. */
+ /* Phase A: register stdlib types/functions (perlfunc + curated CPAN). */
cbm_perl_stdlib_register(&reg, arena);
const char *module_qn = result->module_qn;
- /* Phase B: register functions/methods from this file's defs. Return types
- * are left unknown for the skeleton — real inference lands in plan 22-03. */
+ /* Phase B: register file-local subs (label Function/Method). Return types
+ * are unknown — Perl has no declared types; v1 infers via bless/new at the
+ * call site, not from declarations. */
for (int i = 0; i < result->defs.count; i++) {
CBMDefinition *d = &result->defs.items[i];
if (!d->qualified_name || !d->name || !d->label)
continue;
-
if (strcmp(d->label, "Function") == 0 || strcmp(d->label, "Method") == 0) {
CBMRegisteredFunc rf;
memset(&rf, 0, sizeof(rf));
rf.qualified_name = d->qualified_name;
rf.short_name = d->name;
- if (strcmp(d->label, "Method") == 0 && d->parent_class) {
+ if (strcmp(d->label, "Method") == 0 && d->parent_class)
rf.receiver_type = d->parent_class;
- }
const CBMType **rets =
(const CBMType **)cbm_arena_alloc(arena, 2 * sizeof(const CBMType *));
if (rets) {
@@ -152,17 +1253,36 @@ void cbm_run_perl_lsp(CBMArena *arena, CBMFileResult *result, const char *source
}
}
- /* Phase C: run the resolver. The skeleton initializes context and runs an
- * empty walk; no resolved-call edges are emitted yet (plan 22-03). */
+ /* Phase B.1: pre-pass over the AST to populate the inheritance + import
+ * maps and build per-package types + method tables. This must happen
+ * before resolution (PASS 2) so method dispatch can walk @ISA. The
+ * mutable `reg` lives here; perl_lsp_process_file later runs on the
+ * finished (const) registry. */
PerlLSPContext ctx;
perl_lsp_init(&ctx, arena, source, source_len, &reg, module_qn, &result->resolved_calls);
+
+ ctx.current_package_qn = "";
+ ctx.enclosing_package_qn = "";
+ perl_pass1_scan(&ctx, root);
+ perl_register_packages(&ctx, &reg);
+ perl_attach_methods(&ctx, &reg, root);
+
+ /* Phase C: two-pass resolution walk (PASS 1 re-populates the per-file use
+ * map + ISA context needed for the bless/$self idioms during PASS 2). */
perl_lsp_process_file(&ctx, root);
- /* Silence unused-helper warnings until plan 22-03 wires these in. */
- (void)perl_node_text;
+ if (ctx.debug) {
+ fprintf(stderr, "[perl_lsp] module_qn=%s defs=%d resolved=%d isa=%d types=%d\n",
+ module_qn ? module_qn : "(null)", result->defs.count, result->resolved_calls.count,
+ ctx.isa_count, reg.type_count);
+ for (int i = 0; i < result->resolved_calls.count; i++) {
+ CBMResolvedCall *r = &result->resolved_calls.items[i];
+ fprintf(stderr, "[perl_lsp] %s -> %s [%s %.2f]\n", r->caller_qn, r->callee_qn,
+ r->strategy, r->confidence);
+ }
+ }
+
+ /* Silence unused-helper warnings for API symbols kept for the header /
+ * future cross-file plan. */
(void)perl_pkg_to_dot;
- (void)perl_lsp_add_use;
- (void)perl_eval_expr_type;
- (void)perl_resolve_package_name;
- (void)perl_lookup_method;
}