mirror of
https://github.com/DeusData/codebase-memory-mcp.git
synced 2026-12-03 04:27:30 +08:00
perf(lsp): find imports and registrations by name in eight language passes
The per-language passes scanned the whole type registry per definition
and all of a file's imports per use, and Python copied its import arrays
on every add, so a file's pass grew with the square of its definitions or
imports. Rows are now found by name (lsp_name_rows.h) or through an
opt-in tail index on the registry, and every site falls back to the scan
it replaces when an allocation fails. No cap, budget or early exit is
added, and no lookup may miss what the scan found:
complexity_name_rows_answer_as_a_scan_does and
complexity_registry_tail_answers_as_a_scan_does compare both routes.
Counted work when the input doubles, before and after, per growth test
(all named complexity_*_is_linear or *_are_linear, counted through the
existing lsp_work.h steps):
Python import join 4.00x to 2.13x, wide from-import 4.00x to 1.95x
Go import uses, registration, generic registration 4.00x to 2.00x
Java import uses 3.25x, static imports 3.22x, class registration
3.65x, short-name fallback 3.76x; now 1.55x to 2.00x
Kotlin import uses 3.48x to 2.04x, class registration 3.85x to 2.23x
TypeScript import uses 3.98x to 1.80x, class registration 3.78x to
1.86x, JavaScript registry lookups 3.79x to 1.84x
Rust import uses 3.99x to 1.79x, registration 3.16x to 1.84x,
derive registration 3.46x to 1.81x
C# registration 3.34x to 1.46x
PHP import uses 3.58x, short-name fallback 3.68x, method returns
3.68x; now 2.00x
The two PHP method-return loops start at the lowest row of the existing
method iterator. TypeScript's registry is indexed once the file's
definitions are in, keeping first-registered answers.
No graph content changes: flask, django, helm, gitea, two Java subtrees
of elasticsearch, Exposed, nest, tokio, jellyfin and composer index
identically before and after on one worker. phpunit has no comparison:
it does not finish within 1,700 s on one worker with either binary.
Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
This commit is contained in:
@@ -40,6 +40,8 @@
|
||||
*/
|
||||
|
||||
#include "cs_lsp.h"
|
||||
#include "lsp_name_rows.h"
|
||||
#include "lsp_work.h"
|
||||
#include "lsp_node_iter.h"
|
||||
#include "../helpers.h"
|
||||
#include <ctype.h>
|
||||
@@ -3443,20 +3445,38 @@ typedef struct {
|
||||
const CBMType *rt;
|
||||
} cs_method_rt_entry_t;
|
||||
|
||||
enum { CS_SCRATCH_ARENA_BLOCK = 4096 };
|
||||
|
||||
typedef struct {
|
||||
cs_method_rt_entry_t *items;
|
||||
int count;
|
||||
int cap;
|
||||
/* The rows by QN (lsp_name_rows.h), when the caller gives a scratch arena;
|
||||
* without one, or once a row could not be filed, the dedup scans. */
|
||||
CBMNameRows by_qn;
|
||||
CBMArena *rows_arena;
|
||||
bool rows_failed;
|
||||
} cs_method_rt_table_t;
|
||||
|
||||
static bool cs_method_rt_seen(const cs_method_rt_table_t *tab, const char *qn) {
|
||||
if (tab->rows_arena && !tab->rows_failed) {
|
||||
CBM_LSP_WORK(1);
|
||||
return cbm_name_rows_lookup(&tab->by_qn, NULL, qn) >= 0;
|
||||
}
|
||||
for (int i = 0; i < tab->count; i++) {
|
||||
CBM_LSP_WORK(1);
|
||||
if (strcmp(tab->items[i].qn, qn) == 0)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static void cs_method_rt_add(CBMArena *arena, cs_method_rt_table_t *tab, const char *qn,
|
||||
const CBMType *rt) {
|
||||
if (!qn || !rt)
|
||||
return;
|
||||
for (int i = 0; i < tab->count; i++) {
|
||||
if (strcmp(tab->items[i].qn, qn) == 0)
|
||||
return;
|
||||
}
|
||||
if (cs_method_rt_seen(tab, qn))
|
||||
return;
|
||||
if (tab->count + 1 >= tab->cap) {
|
||||
int new_cap = tab->cap ? tab->cap * 2 : 16;
|
||||
cs_method_rt_entry_t *ne =
|
||||
@@ -3470,9 +3490,49 @@ static void cs_method_rt_add(CBMArena *arena, cs_method_rt_table_t *tab, const c
|
||||
}
|
||||
tab->items[tab->count].qn = qn;
|
||||
tab->items[tab->count].rt = rt;
|
||||
if (tab->rows_arena && !tab->rows_failed &&
|
||||
!cbm_name_rows_add(tab->rows_arena, &tab->by_qn, NULL, qn, tab->count))
|
||||
tab->rows_failed = true;
|
||||
tab->count++;
|
||||
}
|
||||
|
||||
/* The first registration of `qn` among reg's functions or types. `qns` is a
|
||||
* snapshot of their QNs, chained by name (lsp_name_rows.h); nothing registers
|
||||
* while a caller uses it. Without a snapshot this is the scan it replaces. */
|
||||
static int cs_first_func_row(const CBMTypeRegistry *reg, CBMNameRows *rows, CBMArena *arena,
|
||||
const char **qns, const char *qn) {
|
||||
if (qns)
|
||||
return cbm_name_rows_first(rows, arena, qns, reg->func_count, qn);
|
||||
for (int j = 0; j < reg->func_count; j++) {
|
||||
CBM_LSP_WORK(1);
|
||||
if (reg->funcs[j].qualified_name && strcmp(reg->funcs[j].qualified_name, qn) == 0)
|
||||
return j;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int cs_first_type_row(const CBMTypeRegistry *reg, CBMNameRows *rows, CBMArena *arena,
|
||||
const char **qns, const char *qn) {
|
||||
if (qns)
|
||||
return cbm_name_rows_first(rows, arena, qns, reg->type_count, qn);
|
||||
for (int t = 0; t < reg->type_count; t++) {
|
||||
CBM_LSP_WORK(1);
|
||||
if (reg->types[t].qualified_name && strcmp(reg->types[t].qualified_name, qn) == 0)
|
||||
return t;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* The QNs of reg's functions (types: false -> functions), in a scratch arena. */
|
||||
static const char **cs_qn_snapshot(CBMArena *arena, const CBMTypeRegistry *reg, bool types) {
|
||||
int count = types ? reg->type_count : reg->func_count;
|
||||
const char **qns =
|
||||
count > 0 ? (const char **)cbm_arena_alloc(arena, (size_t)count * sizeof(*qns)) : NULL;
|
||||
for (int i = 0; qns && i < count; i++)
|
||||
qns[i] = types ? reg->types[i].qualified_name : reg->funcs[i].qualified_name;
|
||||
return qns;
|
||||
}
|
||||
|
||||
/* Walk the tree, finding method_declaration / property_declaration nodes
|
||||
* and recording (parent_class_qn + "." + method_name → return type). */
|
||||
static void cs_collect_method_return_types(CSLSPContext *ctx, TSNode root,
|
||||
@@ -3585,13 +3645,20 @@ void cbm_cs_refine_ast_return_types(CSLSPContext *ctx, CBMTypeRegistry *reg, TSN
|
||||
if (!ctx || !reg || ts_node_is_null(root))
|
||||
return;
|
||||
|
||||
/* Each method patches the first registration of its QN; the loop starts there. */
|
||||
CBMArena scratch;
|
||||
cbm_arena_init_lazy(&scratch, CS_SCRATCH_ARENA_BLOCK);
|
||||
cs_method_rt_table_t mtab = {0};
|
||||
mtab.rows_arena = &scratch;
|
||||
cs_collect_method_return_types(ctx, root, &mtab);
|
||||
const char **func_qns = mtab.count > 0 ? cs_qn_snapshot(&scratch, reg, false) : NULL;
|
||||
CBMNameRows func_rows = {0};
|
||||
for (int i = 0; i < mtab.count; i++) {
|
||||
const char *qn = mtab.items[i].qn;
|
||||
const CBMType *rt = mtab.items[i].rt;
|
||||
bool patched = false;
|
||||
for (int j = 0; j < reg->func_count; j++) {
|
||||
int first = cs_first_func_row(reg, &func_rows, &scratch, func_qns, qn);
|
||||
for (int j = first >= 0 ? first : reg->func_count; j < reg->func_count; j++) {
|
||||
if (strcmp(reg->funcs[j].qualified_name, qn) == 0) {
|
||||
const CBMType **rets =
|
||||
(const CBMType **)cbm_arena_alloc(ctx->arena, 2 * sizeof(const CBMType *));
|
||||
@@ -3607,6 +3674,7 @@ void cbm_cs_refine_ast_return_types(CSLSPContext *ctx, CBMTypeRegistry *reg, TSN
|
||||
}
|
||||
(void)patched;
|
||||
}
|
||||
cbm_arena_destroy(&scratch);
|
||||
}
|
||||
|
||||
/* ── parse return type from CBMDefinition.return_type ──────────── */
|
||||
@@ -3719,6 +3787,15 @@ void cbm_run_cs_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
|
||||
}
|
||||
}
|
||||
|
||||
/* The registrations are complete: each patch below finds the first registration
|
||||
* of its QN by name instead of scanning them all per definition. */
|
||||
CBMArena scratch;
|
||||
cbm_arena_init_lazy(&scratch, CS_SCRATCH_ARENA_BLOCK);
|
||||
const char **func_qns = cs_qn_snapshot(&scratch, ®, false);
|
||||
const char **type_qns = cs_qn_snapshot(&scratch, ®, true);
|
||||
CBMNameRows func_rows = {0};
|
||||
CBMNameRows type_rows = {0};
|
||||
|
||||
/* Phase C: build context for type resolution + run the resolver. */
|
||||
CSLSPContext ctx;
|
||||
cs_lsp_init(&ctx, arena, source, source_len, ®, module_qn, &result->resolved_calls);
|
||||
@@ -3741,7 +3818,8 @@ void cbm_run_cs_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
|
||||
const CBMType **param_types = cbm_type_materialize_signature_params(
|
||||
arena, d->signature_param_types, d->signature_param_count,
|
||||
cs_signature_param_type_adapter, &ctx);
|
||||
for (int j = 0; j < reg.func_count; j++) {
|
||||
int first = cs_first_func_row(®, &func_rows, &scratch, func_qns, d->qualified_name);
|
||||
for (int j = first >= 0 ? first : reg.func_count; j < reg.func_count; j++) {
|
||||
if (strcmp(reg.funcs[j].qualified_name, d->qualified_name) != 0)
|
||||
continue;
|
||||
const CBMType **return_types = NULL;
|
||||
@@ -3763,7 +3841,8 @@ void cbm_run_cs_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
|
||||
continue;
|
||||
const CBMType *rt = cs_parse_return_type_text(&ctx, d->return_type);
|
||||
/* Find the registered func and patch its signature. */
|
||||
for (int j = 0; j < reg.func_count; j++) {
|
||||
int first = cs_first_func_row(®, &func_rows, &scratch, func_qns, d->qualified_name);
|
||||
for (int j = first >= 0 ? first : reg.func_count; j < reg.func_count; j++) {
|
||||
if (strcmp(reg.funcs[j].qualified_name, d->qualified_name) == 0) {
|
||||
const CBMType **rets =
|
||||
(const CBMType **)cbm_arena_alloc(arena, 2 * sizeof(const CBMType *));
|
||||
@@ -3791,7 +3870,8 @@ void cbm_run_cs_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
|
||||
cs_fields_t *f = &tab.items[i];
|
||||
if (f->count == 0)
|
||||
continue;
|
||||
for (int t = 0; t < reg.type_count; t++) {
|
||||
int first = cs_first_type_row(®, &type_rows, &scratch, type_qns, f->class_qn);
|
||||
for (int t = first >= 0 ? first : reg.type_count; t < reg.type_count; t++) {
|
||||
if (strcmp(reg.types[t].qualified_name, f->class_qn) == 0) {
|
||||
reg.types[t].field_names = f->field_names;
|
||||
reg.types[t].field_types = f->field_types;
|
||||
@@ -3800,6 +3880,7 @@ void cbm_run_cs_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
|
||||
}
|
||||
}
|
||||
}
|
||||
cbm_arena_destroy(&scratch);
|
||||
|
||||
/* Re-bind ctx state since collect_class_fields may have temporarily
|
||||
* adjusted namespace stack (it doesn't, but be safe). */
|
||||
|
||||
+132
-51
@@ -1,5 +1,7 @@
|
||||
#include "go_lsp.h"
|
||||
#include "lsp_node_iter.h"
|
||||
#include "lsp_neg_memo.h"
|
||||
#include "lsp_work.h"
|
||||
#include "../../../src/foundation/hash_table.h"
|
||||
#include "../helpers.h"
|
||||
#include <string.h>
|
||||
@@ -82,13 +84,14 @@ static char *lsp_node_text(GoLSPContext *ctx, TSNode node) {
|
||||
|
||||
// --- Helper: resolve import alias to package QN ---
|
||||
|
||||
/* The first import of this local name. Found by name (lsp_name_rows.h): a scan
|
||||
* per use made a file of thousands of imports quadratic. */
|
||||
static const char *resolve_import(GoLSPContext *ctx, const char *local_name) {
|
||||
for (int i = 0; i < ctx->import_count; i++) {
|
||||
if (strcmp(ctx->import_local_names[i], local_name) == 0) {
|
||||
return ctx->import_package_qns[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
if (!local_name)
|
||||
return NULL;
|
||||
int i = cbm_name_rows_first(&ctx->import_rows, ctx->arena, ctx->import_local_names,
|
||||
ctx->import_count, local_name);
|
||||
return i >= 0 ? ctx->import_package_qns[i] : NULL;
|
||||
}
|
||||
|
||||
// --- Helper: check if name is a Go builtin ---
|
||||
@@ -2263,6 +2266,47 @@ static void go_registry_with_stdlib(CBMTypeRegistry *reg, CBMArena *arena) {
|
||||
}
|
||||
}
|
||||
|
||||
/* The first row of each type QN in a filling registry, as a scan from the top
|
||||
* finds it. Registering and refining a file's types asked a scan of every type
|
||||
* per definition, and a generated file holds thousands. A row the memo cannot
|
||||
* take (no memory) sends every later question back to the scan. */
|
||||
enum { GO_MEMO_ARENA_BLOCK = 4096 };
|
||||
|
||||
typedef struct {
|
||||
CBMIdxMemo first;
|
||||
CBMArena *arena;
|
||||
bool failed;
|
||||
} GoTypeRows;
|
||||
|
||||
static void go_type_rows_note(GoTypeRows *rows, const CBMTypeRegistry *reg, int row) {
|
||||
const char *qn = reg->types[row].qualified_name;
|
||||
if (rows->failed || !qn)
|
||||
return;
|
||||
CBM_LSP_WORK(1);
|
||||
cbm_idxmemo_put_if_absent(&rows->first, rows->arena, qn, row);
|
||||
if (cbm_idxmemo_get(&rows->first, qn) < 0)
|
||||
rows->failed = true;
|
||||
}
|
||||
|
||||
static void go_type_rows_add(CBMTypeRegistry *reg, GoTypeRows *rows, CBMRegisteredType type) {
|
||||
int before = reg->type_count;
|
||||
cbm_registry_add_type(reg, type);
|
||||
if (reg->type_count > before)
|
||||
go_type_rows_note(rows, reg, reg->type_count - 1);
|
||||
}
|
||||
|
||||
static int go_type_rows_first(const GoTypeRows *rows, const CBMTypeRegistry *reg, const char *qn) {
|
||||
CBM_LSP_WORK(1);
|
||||
if (!rows->failed)
|
||||
return cbm_idxmemo_get(&rows->first, qn);
|
||||
for (int ti = 0; ti < reg->type_count; ti++) {
|
||||
CBM_LSP_WORK(1);
|
||||
if (reg->types[ti].qualified_name && strcmp(reg->types[ti].qualified_name, qn) == 0)
|
||||
return ti;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// --- Entry point: build registry from file defs + run LSP ---
|
||||
|
||||
void cbm_run_go_lsp(CBMArena *arena, CBMFileResult *result, const char *source, int source_len,
|
||||
@@ -2274,6 +2318,14 @@ void cbm_run_go_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
|
||||
// Go stdlib types/functions: the shared sealed registry, chained
|
||||
go_registry_with_stdlib(®, arena);
|
||||
|
||||
/* Rows of the file's own registry by QN, from whatever it holds already (the
|
||||
* stdlib, when it could not be shared) to the types registered below. */
|
||||
CBMArena memo_arena;
|
||||
cbm_arena_init_lazy(&memo_arena, GO_MEMO_ARENA_BLOCK);
|
||||
GoTypeRows own_types = {.arena = &memo_arena};
|
||||
for (int ti = 0; ti < reg.type_count; ti++)
|
||||
go_type_rows_note(&own_types, ®, ti);
|
||||
|
||||
const char *module_qn = result->module_qn;
|
||||
|
||||
// Phase 1: Register all types and functions from the file's own definitions
|
||||
@@ -2313,7 +2365,7 @@ void cbm_run_go_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
|
||||
}
|
||||
}
|
||||
|
||||
cbm_registry_add_type(®, rt);
|
||||
go_type_rows_add(®, &own_types, rt);
|
||||
}
|
||||
|
||||
// Register Function/Method nodes
|
||||
@@ -2402,13 +2454,16 @@ void cbm_run_go_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
|
||||
}
|
||||
}
|
||||
|
||||
if (rf.receiver_type && !cbm_registry_lookup_type(®, rf.receiver_type)) {
|
||||
/* reg's own types are all in own_types; the stdlib is behind. */
|
||||
if (rf.receiver_type &&
|
||||
go_type_rows_first(&own_types, ®, rf.receiver_type) < 0 &&
|
||||
!cbm_registry_lookup_type(reg.fallback, rf.receiver_type)) {
|
||||
const char *dot = strrchr(rf.receiver_type, '.');
|
||||
CBMRegisteredType auto_type;
|
||||
memset(&auto_type, 0, sizeof(auto_type));
|
||||
auto_type.qualified_name = rf.receiver_type;
|
||||
auto_type.short_name = dot ? dot + 1 : rf.receiver_type;
|
||||
cbm_registry_add_type(®, auto_type);
|
||||
go_type_rows_add(®, &own_types, auto_type);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2446,22 +2501,16 @@ void cbm_run_go_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
|
||||
cbm_arena_sprintf(arena, "%s.%s", module_qn, aname);
|
||||
const char *alias_target_qn =
|
||||
cbm_arena_sprintf(arena, "%s.%s", module_qn, atarget);
|
||||
bool found_a = false;
|
||||
for (int ti = 0; ti < reg.type_count; ti++) {
|
||||
if (reg.types[ti].qualified_name &&
|
||||
strcmp(reg.types[ti].qualified_name, alias_type_qn) == 0) {
|
||||
reg.types[ti].alias_of = alias_target_qn;
|
||||
found_a = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found_a) {
|
||||
int alias_at = go_type_rows_first(&own_types, ®, alias_type_qn);
|
||||
if (alias_at >= 0) {
|
||||
reg.types[alias_at].alias_of = alias_target_qn;
|
||||
} else {
|
||||
CBMRegisteredType alias_rt;
|
||||
memset(&alias_rt, 0, sizeof(alias_rt));
|
||||
alias_rt.qualified_name = alias_type_qn;
|
||||
alias_rt.short_name = aname;
|
||||
alias_rt.alias_of = alias_target_qn;
|
||||
cbm_registry_add_type(®, alias_rt);
|
||||
go_type_rows_add(®, &own_types, alias_rt);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2502,20 +2551,16 @@ void cbm_run_go_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
|
||||
}
|
||||
}
|
||||
}
|
||||
if (iface_method_count > 0) {
|
||||
for (int ti = 0; ti < reg.type_count; ti++) {
|
||||
if (!reg.types[ti].qualified_name ||
|
||||
strcmp(reg.types[ti].qualified_name, type_qn) != 0)
|
||||
continue;
|
||||
const char **names = (const char **)cbm_arena_alloc(
|
||||
arena, (iface_method_count + 1) * sizeof(const char *));
|
||||
for (int mi = 0; mi < iface_method_count; mi++) {
|
||||
names[mi] = iface_methods[mi];
|
||||
}
|
||||
names[iface_method_count] = NULL;
|
||||
reg.types[ti].method_names = names;
|
||||
break;
|
||||
int iface_at =
|
||||
iface_method_count > 0 ? go_type_rows_first(&own_types, ®, type_qn) : -1;
|
||||
if (iface_at >= 0) {
|
||||
const char **names = (const char **)cbm_arena_alloc(
|
||||
arena, (iface_method_count + 1) * sizeof(const char *));
|
||||
for (int mi = 0; mi < iface_method_count; mi++) {
|
||||
names[mi] = iface_methods[mi];
|
||||
}
|
||||
names[iface_method_count] = NULL;
|
||||
reg.types[iface_at].method_names = names;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@@ -2585,11 +2630,8 @@ void cbm_run_go_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
|
||||
}
|
||||
|
||||
// Find the registered type and update it
|
||||
for (int ti = 0; ti < reg.type_count; ti++) {
|
||||
if (!reg.types[ti].qualified_name ||
|
||||
strcmp(reg.types[ti].qualified_name, type_qn) != 0)
|
||||
continue;
|
||||
|
||||
int ti = go_type_rows_first(&own_types, ®, type_qn);
|
||||
if (ti >= 0) {
|
||||
if (embed_count > 0) {
|
||||
const char **arr = (const char **)cbm_arena_alloc(
|
||||
arena, (embed_count + 1) * sizeof(const char *));
|
||||
@@ -2612,11 +2654,11 @@ void cbm_run_go_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
|
||||
reg.types[ti].field_names = names;
|
||||
reg.types[ti].field_types = types;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
cbm_arena_destroy(&memo_arena);
|
||||
|
||||
// Phase 1c: Extract type parameters from generic function declarations
|
||||
extract_type_params_from_ast(arena, ®, root, source, module_qn);
|
||||
@@ -3033,10 +3075,45 @@ static const CBMType *cbm_go_lsp_keep_known_refinement(const CBMType *candidate,
|
||||
return candidate ? candidate : cbm_type_unknown();
|
||||
}
|
||||
|
||||
/* The first registration of each function QN, listed when the first generic
|
||||
* function asks: each one scanned every registered function for its own. The
|
||||
* functions do not change while they are patched. */
|
||||
typedef struct {
|
||||
CBMIdxMemo first;
|
||||
CBMArena arena;
|
||||
bool listed;
|
||||
bool failed; /* a row could not be remembered: scan instead */
|
||||
} GoFuncRows;
|
||||
|
||||
static int go_func_rows_first(GoFuncRows *rows, const CBMTypeRegistry *reg, const char *qn) {
|
||||
for (int fi = 0; !rows->listed && !rows->failed && fi < reg->func_count; fi++) {
|
||||
const char *name = reg->funcs[fi].qualified_name;
|
||||
if (!name)
|
||||
continue;
|
||||
CBM_LSP_WORK(1);
|
||||
cbm_idxmemo_put_if_absent(&rows->first, &rows->arena, name, fi);
|
||||
if (cbm_idxmemo_get(&rows->first, name) < 0)
|
||||
rows->failed = true;
|
||||
}
|
||||
rows->listed = true;
|
||||
CBM_LSP_WORK(1);
|
||||
if (!rows->failed)
|
||||
return cbm_idxmemo_get(&rows->first, qn);
|
||||
for (int fi = 0; fi < reg->func_count; fi++) {
|
||||
CBM_LSP_WORK(1);
|
||||
if (reg->funcs[fi].qualified_name && strcmp(reg->funcs[fi].qualified_name, qn) == 0)
|
||||
return fi;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Scan AST for generic function declarations and set type_param_names + re-parse
|
||||
// return types on matching registered functions.
|
||||
static void extract_type_params_from_ast(CBMArena *arena, CBMTypeRegistry *reg, TSNode root,
|
||||
const char *source, const char *module_qn) {
|
||||
GoFuncRows func_rows;
|
||||
memset(&func_rows, 0, sizeof(func_rows));
|
||||
cbm_arena_init_lazy(&func_rows.arena, GO_MEMO_ARENA_BLOCK);
|
||||
|
||||
// Collect top-level children once (O(n)); ts_node_child(root,i) is O(i) → O(n²).
|
||||
uint32_t rkn = 0;
|
||||
@@ -3101,7 +3178,8 @@ static void extract_type_params_from_ast(CBMArena *arena, CBMTypeRegistry *reg,
|
||||
|
||||
// Find the matching registered function and set type_param_names
|
||||
const char *func_qn = cbm_arena_sprintf(arena, "%s.%s", module_qn, func_name);
|
||||
for (int fi = 0; fi < reg->func_count; fi++) {
|
||||
int first_fi = go_func_rows_first(&func_rows, reg, func_qn);
|
||||
for (int fi = first_fi >= 0 ? first_fi : reg->func_count; fi < reg->func_count; fi++) {
|
||||
if (reg->funcs[fi].qualified_name &&
|
||||
strcmp(reg->funcs[fi].qualified_name, func_qn) == 0) {
|
||||
reg->funcs[fi].type_param_names = tp_names;
|
||||
@@ -3239,6 +3317,7 @@ static void extract_type_params_from_ast(CBMArena *arena, CBMTypeRegistry *reg,
|
||||
}
|
||||
}
|
||||
}
|
||||
cbm_arena_destroy(&func_rows.arena);
|
||||
}
|
||||
|
||||
// Forward: tree-sitter Go language (from lang_specs.c)
|
||||
@@ -3345,6 +3424,19 @@ void cbm_run_go_lsp_cross(CBMArena *arena, const char *source, int source_len,
|
||||
|
||||
// 3. Phase 1b: Scan AST for struct definitions to populate embedded_types
|
||||
{
|
||||
/* One context parses every struct's field types, so its import rows are
|
||||
* chained once (resolve_import), not once per struct. */
|
||||
GoLSPContext tmp_ctx;
|
||||
memset(&tmp_ctx, 0, sizeof(tmp_ctx));
|
||||
tmp_ctx.arena = arena;
|
||||
tmp_ctx.source = source;
|
||||
tmp_ctx.source_len = source_len;
|
||||
tmp_ctx.registry = ®
|
||||
tmp_ctx.package_qn = module_qn;
|
||||
tmp_ctx.import_local_names = import_names;
|
||||
tmp_ctx.import_package_qns = import_qns;
|
||||
tmp_ctx.import_count = import_count;
|
||||
|
||||
// Collect top-level children once (O(n)); ts_node_child(root,i) is O(i) → O(n²).
|
||||
uint32_t rkn = 0;
|
||||
TSNode *rkids = cbm_lsp_collect_children(arena, root, &rkn);
|
||||
@@ -3422,17 +3514,6 @@ void cbm_run_go_lsp_cross(CBMArena *arena, const char *source, int source_len,
|
||||
const CBMType *fld_types[64];
|
||||
int fld_count = 0;
|
||||
|
||||
GoLSPContext tmp_ctx;
|
||||
memset(&tmp_ctx, 0, sizeof(tmp_ctx));
|
||||
tmp_ctx.arena = arena;
|
||||
tmp_ctx.source = source;
|
||||
tmp_ctx.source_len = source_len;
|
||||
tmp_ctx.registry = ®
|
||||
tmp_ctx.package_qn = module_qn;
|
||||
tmp_ctx.import_local_names = import_names;
|
||||
tmp_ctx.import_package_qns = import_qns;
|
||||
tmp_ctx.import_count = import_count;
|
||||
|
||||
uint32_t fl_nc = ts_node_child_count(field_list);
|
||||
for (uint32_t k = 0; k < fl_nc; k++) {
|
||||
TSNode field = ts_node_child(field_list, k);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef CBM_LSP_GO_LSP_H
|
||||
#define CBM_LSP_GO_LSP_H
|
||||
|
||||
#include "lsp_name_rows.h"
|
||||
#include "type_rep.h"
|
||||
#include "scope.h"
|
||||
#include "type_registry.h"
|
||||
@@ -18,6 +19,7 @@ typedef struct {
|
||||
const char** import_local_names; // NULL-terminated
|
||||
const char** import_package_qns; // NULL-terminated
|
||||
int import_count;
|
||||
CBMNameRows import_rows; // import_local_names by name, rows in table order
|
||||
|
||||
// Current function context
|
||||
const char* enclosing_func_qn;
|
||||
|
||||
+74
-27
@@ -32,6 +32,7 @@
|
||||
*/
|
||||
|
||||
#include "java_lsp.h"
|
||||
#include "lsp_work.h"
|
||||
#include "../helpers.h"
|
||||
|
||||
#include <ctype.h>
|
||||
@@ -318,6 +319,18 @@ void java_lsp_add_import(JavaLSPContext *ctx, const char *local_name, const char
|
||||
ctx->import_count++;
|
||||
}
|
||||
|
||||
/* The imports with this local name, in table order (lsp_name_rows.h): a scan per
|
||||
* use made a file of thousands of imports quadratic. */
|
||||
static int java_import_first(JavaLSPContext *ctx, const char *name) {
|
||||
return cbm_name_rows_first(&ctx->import_rows, ctx->arena, ctx->import_local_names,
|
||||
ctx->import_count, name);
|
||||
}
|
||||
|
||||
static int java_import_next(const JavaLSPContext *ctx, const char *name, int row) {
|
||||
return cbm_name_rows_next(&ctx->import_rows, ctx->import_local_names, ctx->import_count, name,
|
||||
row);
|
||||
}
|
||||
|
||||
static void push_enclosing_class(JavaLSPContext *ctx, const char *class_qn) {
|
||||
if (ctx->enclosing_class_depth >= ctx->enclosing_class_cap) {
|
||||
int new_cap = ctx->enclosing_class_cap == 0 ? 8 : ctx->enclosing_class_cap * 2;
|
||||
@@ -502,7 +515,7 @@ const char *java_resolve_type_name(JavaLSPContext *ctx, const char *name) {
|
||||
}
|
||||
|
||||
/* 2. Single-type imports. */
|
||||
for (int i = 0; i < ctx->import_count; i++) {
|
||||
for (int i = java_import_first(ctx, name); i >= 0; i = java_import_next(ctx, name, i)) {
|
||||
if (ctx->import_kinds[i] != CBM_JAVA_IMPORT_TYPE)
|
||||
continue;
|
||||
if (strcmp(ctx->import_local_names[i], name) == 0) {
|
||||
@@ -563,7 +576,12 @@ const char *java_resolve_type_name(JavaLSPContext *ctx, const char *name) {
|
||||
if (ctx->registry && ctx->registry->types) {
|
||||
const char *only_qn = NULL;
|
||||
int matches = 0;
|
||||
for (int i = 0; i < ctx->registry->type_count && matches < 2; i++) {
|
||||
/* The registry's own types of this short name: by name once it is indexed
|
||||
* (cbm_registry_index_types_into), else every type, as before. */
|
||||
CBMTypeShortIter named;
|
||||
cbm_registry_types_by_short_name(ctx->registry, name, &named);
|
||||
for (int i = -1; matches < 2 && (i = cbm_type_short_iter_next(&named)) >= 0;) {
|
||||
CBM_LSP_WORK(1);
|
||||
const CBMRegisteredType *t = &ctx->registry->types[i];
|
||||
if (!t->qualified_name || !t->short_name || t->alias_of) {
|
||||
continue;
|
||||
@@ -818,7 +836,7 @@ static const CBMType *resolve_identifier_type(JavaLSPContext *ctx, const char *n
|
||||
}
|
||||
|
||||
/* Static imports — could be a static field. */
|
||||
for (int i = 0; i < ctx->import_count; i++) {
|
||||
for (int i = java_import_first(ctx, name); i >= 0; i = java_import_next(ctx, name, i)) {
|
||||
if (ctx->import_kinds[i] != CBM_JAVA_IMPORT_STATIC)
|
||||
continue;
|
||||
if (strcmp(ctx->import_local_names[i], name) == 0) {
|
||||
@@ -1121,7 +1139,7 @@ static const CBMType *eval_method_invocation(JavaLSPContext *ctx, TSNode node) {
|
||||
if (f)
|
||||
return java_return_type_of(f);
|
||||
}
|
||||
for (int i = 0; i < ctx->import_count; i++) {
|
||||
for (int i = java_import_first(ctx, mname); i >= 0; i = java_import_next(ctx, mname, i)) {
|
||||
if (ctx->import_kinds[i] != CBM_JAVA_IMPORT_STATIC)
|
||||
continue;
|
||||
if (strcmp(ctx->import_local_names[i], mname) != 0)
|
||||
@@ -2148,7 +2166,7 @@ static void resolve_method_call(JavaLSPContext *ctx, TSNode call) {
|
||||
}
|
||||
}
|
||||
/* Static import. */
|
||||
for (int i = 0; i < ctx->import_count; i++) {
|
||||
for (int i = java_import_first(ctx, mname); i >= 0; i = java_import_next(ctx, mname, i)) {
|
||||
if (ctx->import_kinds[i] != CBM_JAVA_IMPORT_STATIC)
|
||||
continue;
|
||||
if (strcmp(ctx->import_local_names[i], mname) != 0)
|
||||
@@ -3359,6 +3377,10 @@ static void register_local_func_or_type_from_file(JavaLSPContext *ctx, CBMTypeRe
|
||||
: d->qualified_name;
|
||||
cbm_registry_add_type(reg, stub);
|
||||
}
|
||||
/* Every local type is in: from here on a type is found by name, not by a scan of
|
||||
* every registered type per definition (first registrations, as the scan). */
|
||||
if (reg->tail_arena)
|
||||
cbm_registry_index_types_into(reg, reg->tail_arena);
|
||||
|
||||
/* Pass 1b: now that every local type is in the registry, walk again and
|
||||
* fill in embedded_types using the parent_class chain. We mutate the
|
||||
@@ -3383,15 +3405,7 @@ static void register_local_func_or_type_from_file(JavaLSPContext *ctx, CBMTypeRe
|
||||
do the in-place update directly. */
|
||||
(void)rt;
|
||||
/* Locate the actual entry in reg->types[] and write into it. */
|
||||
CBMRegisteredType *slot = NULL;
|
||||
for (int ti = 0; ti < reg->type_count; ti++) {
|
||||
if (reg->types[ti].qualified_name == d->qualified_name ||
|
||||
(reg->types[ti].qualified_name &&
|
||||
strcmp(reg->types[ti].qualified_name, d->qualified_name) == 0)) {
|
||||
slot = ®->types[ti];
|
||||
break;
|
||||
}
|
||||
}
|
||||
CBMRegisteredType *slot = cbm_registry_first_own_type(reg, d->qualified_name);
|
||||
if (!slot)
|
||||
continue;
|
||||
if (d->base_classes) {
|
||||
@@ -3563,6 +3577,25 @@ static void register_local_func_or_type_from_file(JavaLSPContext *ctx, CBMTypeRe
|
||||
static void patch_method_signatures_from_ast(JavaLSPContext *ctx, CBMTypeRegistry *reg,
|
||||
TSNode class_node, const char *enclosing_class_qn);
|
||||
|
||||
/* The next registered function of this QN after `row` (-1: the first), in
|
||||
* registration order. By name when the caller listed the functions in
|
||||
* ctx->func_names and none was added since; otherwise by a scan. */
|
||||
static int java_func_row_next(JavaLSPContext *ctx, const CBMTypeRegistry *reg, const char *qn,
|
||||
int row) {
|
||||
if (ctx->func_names && ctx->func_name_count == reg->func_count) {
|
||||
return row < 0 ? cbm_name_rows_first(&ctx->func_rows, ctx->func_rows_arena, ctx->func_names,
|
||||
ctx->func_name_count, qn)
|
||||
: cbm_name_rows_next(&ctx->func_rows, ctx->func_names, ctx->func_name_count,
|
||||
qn, row);
|
||||
}
|
||||
for (int fi = row + 1; fi < reg->func_count; fi++) {
|
||||
CBM_LSP_WORK(1);
|
||||
if (reg->funcs[fi].qualified_name && strcmp(reg->funcs[fi].qualified_name, qn) == 0)
|
||||
return fi;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void patch_one_method(JavaLSPContext *ctx, CBMTypeRegistry *reg, TSNode method_node,
|
||||
const char *class_qn, bool is_constructor) {
|
||||
TSNode name_node = ts_node_child_by_field_name(method_node, "name", 4);
|
||||
@@ -3608,7 +3641,8 @@ static void patch_one_method(JavaLSPContext *ctx, CBMTypeRegistry *reg, TSNode m
|
||||
* first QN match keeps the legacy no-signature path best-effort. */
|
||||
CBMRegisteredFunc *slot = NULL;
|
||||
CBMRegisteredFunc *first_qn_match = NULL;
|
||||
for (int fi = 0; fi < reg->func_count; fi++) {
|
||||
for (int fi = java_func_row_next(ctx, reg, method_qn, -1); fi >= 0;
|
||||
fi = java_func_row_next(ctx, reg, method_qn, fi)) {
|
||||
CBMRegisteredFunc *candidate = ®->funcs[fi];
|
||||
if (!candidate->qualified_name || strcmp(candidate->qualified_name, method_qn) != 0)
|
||||
continue;
|
||||
@@ -3709,13 +3743,7 @@ static void patch_method_signatures_from_ast(JavaLSPContext *ctx, CBMTypeRegistr
|
||||
* `obj.field.method()` for arbitrary receivers, not just `this`. */
|
||||
static void append_field_to_class(CBMTypeRegistry *reg, CBMArena *a, const char *class_qn,
|
||||
const char *field_name, const CBMType *ftype) {
|
||||
CBMRegisteredType *slot = NULL;
|
||||
for (int ti = 0; ti < reg->type_count; ti++) {
|
||||
if (reg->types[ti].qualified_name && strcmp(reg->types[ti].qualified_name, class_qn) == 0) {
|
||||
slot = ®->types[ti];
|
||||
break;
|
||||
}
|
||||
}
|
||||
CBMRegisteredType *slot = cbm_registry_first_own_type(reg, class_qn);
|
||||
if (!slot)
|
||||
return;
|
||||
|
||||
@@ -3813,6 +3841,8 @@ static void populate_class_fields_from_ast(JavaLSPContext *ctx, CBMTypeRegistry
|
||||
|
||||
extern const TSLanguage *tree_sitter_java(void);
|
||||
|
||||
enum { JAVA_SCRATCH_ARENA_BLOCK = 4096 };
|
||||
|
||||
void cbm_run_java_lsp(CBMArena *arena, CBMFileResult *result, const char *source, int source_len,
|
||||
TSNode root) {
|
||||
if (!arena || !result || !source)
|
||||
@@ -3846,9 +3876,23 @@ void cbm_run_java_lsp(CBMArena *arena, CBMFileResult *result, const char *source
|
||||
java_lsp_add_import(&ctx, imp->local_name, imp->module_path, CBM_JAVA_IMPORT_TYPE);
|
||||
}
|
||||
|
||||
/* Register file-local defs into the registry. */
|
||||
/* Register file-local defs into the registry. Its lookups run while it
|
||||
* fills, so it indexes its types as they come, in a scratch arena. */
|
||||
CBMArena scratch;
|
||||
cbm_arena_init_lazy(&scratch, JAVA_SCRATCH_ARENA_BLOCK);
|
||||
reg.tail_arena = &scratch;
|
||||
register_local_func_or_type_from_file(&ctx, ®, result);
|
||||
|
||||
/* Overloads share a QN, so patching a method asks for all of its registrations. */
|
||||
ctx.func_rows_arena = &scratch;
|
||||
ctx.func_names =
|
||||
reg.func_count > 0
|
||||
? (const char **)cbm_arena_alloc(&scratch, (size_t)reg.func_count * sizeof(char *))
|
||||
: NULL;
|
||||
ctx.func_name_count = ctx.func_names ? reg.func_count : 0;
|
||||
for (int fi = 0; fi < ctx.func_name_count; fi++)
|
||||
ctx.func_names[fi] = reg.funcs[fi].qualified_name;
|
||||
|
||||
/* Populate field metadata from the AST so eval_field_access works on
|
||||
* receivers other than `this`. Also patch method signatures with
|
||||
* generics-preserving types (extract_defs strips generics from
|
||||
@@ -3863,9 +3907,12 @@ void cbm_run_java_lsp(CBMArena *arena, CBMFileResult *result, const char *source
|
||||
patch_method_signatures_from_ast(&ctx, ®, c, NULL);
|
||||
}
|
||||
}
|
||||
ctx.func_names = NULL;
|
||||
ctx.func_name_count = 0;
|
||||
|
||||
/* Walk the file. */
|
||||
java_lsp_process_file(&ctx, root);
|
||||
cbm_arena_destroy(&scratch);
|
||||
}
|
||||
|
||||
/* ── Cross-file LSP ───────────────────────────────────────────────── */
|
||||
@@ -4065,14 +4112,14 @@ void cbm_run_java_lsp_cross_with_registry(CBMArena *arena, CBMFileResult *result
|
||||
JavaLSPContext ctx;
|
||||
java_lsp_init(&ctx, arena, source, source_len, &overlay, NULL, module_qn, out);
|
||||
|
||||
register_local_func_or_type_from_file(&ctx, &overlay, result);
|
||||
cbm_pxc_count_perfile_defs((uint64_t)result->defs.count);
|
||||
|
||||
/* Index the overlay only — the base is already finalized. Scratch arena so
|
||||
* per-file bucket allocations do not accumulate in the pipeline-lifetime
|
||||
* arena across a large repo. */
|
||||
* arena across a large repo. Registration already finds its own types there. */
|
||||
CBMArena idx_arena;
|
||||
cbm_arena_init(&idx_arena);
|
||||
overlay.tail_arena = &idx_arena;
|
||||
register_local_func_or_type_from_file(&ctx, &overlay, result);
|
||||
cbm_pxc_count_perfile_defs((uint64_t)result->defs.count);
|
||||
cbm_registry_finalize_into(&overlay, &idx_arena);
|
||||
|
||||
TSTree *tree = cached_tree;
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
* enforced by lsp_resolve.h).
|
||||
*/
|
||||
|
||||
#include "lsp_name_rows.h"
|
||||
#include "type_rep.h"
|
||||
#include "scope.h"
|
||||
#include "type_registry.h"
|
||||
@@ -72,6 +73,14 @@ typedef struct {
|
||||
int *import_kinds;
|
||||
int import_count;
|
||||
int import_cap;
|
||||
CBMNameRows import_rows; /* import_local_names by name, rows in table order */
|
||||
|
||||
/* While signatures are patched: the registered functions' QNs, by name
|
||||
* (overloads share a QN). NULL func_names: a scan answers instead. */
|
||||
const char **func_names;
|
||||
int func_name_count;
|
||||
CBMNameRows func_rows;
|
||||
CBMArena *func_rows_arena;
|
||||
|
||||
/* Current enclosing context. */
|
||||
const char *enclosing_method_qn; /* QN of the nearest method/ctor */
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
*/
|
||||
|
||||
#include "kotlin_lsp.h"
|
||||
#include "lsp_work.h"
|
||||
#include "../helpers.h"
|
||||
/* CBMHashTable — cross-registry field bucketing. Relative path: the LSP
|
||||
* amalgamation compiles with -Iinternal/cbm only, not -Isrc. */
|
||||
@@ -63,6 +64,7 @@ typedef struct {
|
||||
|
||||
#define KT_EVAL_MAX_DEPTH 32
|
||||
#define KT_IMPORT_INITIAL_CAP 16
|
||||
#define KT_INDEX_ARENA_BLOCK 4096
|
||||
|
||||
#define KT_CONF_CONSTRUCTOR 0.95f
|
||||
#define KT_CONF_METHOD 0.90f
|
||||
@@ -1011,6 +1013,17 @@ void kotlin_lsp_add_import(KotlinLSPContext *ctx, const char *local_name, const
|
||||
ctx->import_count++;
|
||||
}
|
||||
|
||||
/* The imports with this local name, in table order (lsp_name_rows.h): a scan per
|
||||
* use made a file of thousands of imports quadratic. */
|
||||
static int kt_import_first(KotlinLSPContext *ctx, const char *name) {
|
||||
return cbm_name_rows_first(&ctx->import_rows, ctx->arena, ctx->import_locals, ctx->import_count,
|
||||
name);
|
||||
}
|
||||
|
||||
static int kt_import_next(const KotlinLSPContext *ctx, const char *name, int row) {
|
||||
return cbm_name_rows_next(&ctx->import_rows, ctx->import_locals, ctx->import_count, name, row);
|
||||
}
|
||||
|
||||
const char *kotlin_resolve_class_name(KotlinLSPContext *ctx, const char *name) {
|
||||
if (!ctx || !name || !*name) {
|
||||
return NULL;
|
||||
@@ -1041,7 +1054,7 @@ const char *kotlin_resolve_class_name(KotlinLSPContext *ctx, const char *name) {
|
||||
}
|
||||
|
||||
/* Explicit imports */
|
||||
for (int i = 0; i < ctx->import_count; i++) {
|
||||
for (int i = kt_import_first(ctx, name); i >= 0; i = kt_import_next(ctx, name, i)) {
|
||||
const char *local = ctx->import_locals[i];
|
||||
if (local && strcmp(local, name) == 0 &&
|
||||
(ctx->import_kinds[i] == CBM_KT_USE_TYPE ||
|
||||
@@ -1103,7 +1116,7 @@ const char *kotlin_resolve_function_name(KotlinLSPContext *ctx, const char *name
|
||||
* symbol itself. Prove that the candidate is a registered top-level
|
||||
* function before accepting it; otherwise `import values.handler` where
|
||||
* handler is a property would fabricate a callable target. */
|
||||
for (int i = 0; i < ctx->import_count; i++) {
|
||||
for (int i = kt_import_first(ctx, name); i >= 0; i = kt_import_next(ctx, name, i)) {
|
||||
const char *local = ctx->import_locals[i];
|
||||
if (local && strcmp(local, name) == 0 &&
|
||||
(ctx->import_kinds[i] == CBM_KT_USE_FUNCTION ||
|
||||
@@ -5189,9 +5202,12 @@ void cbm_run_kotlin_lsp(CBMArena *arena, CBMFileResult *result, const char *sour
|
||||
}
|
||||
}
|
||||
|
||||
/* Build per-file registry. */
|
||||
/* Build per-file registry. Indexed once the file's declarations are in, as the
|
||||
* cross-file pass does: a scan finds the first registration, so the index
|
||||
* answers with that one too. */
|
||||
CBMTypeRegistry registry;
|
||||
cbm_registry_init(®istry, arena);
|
||||
registry.index_first_registered = true;
|
||||
|
||||
/* Curated stdlib */
|
||||
cbm_kotlin_stdlib_register(®istry, arena);
|
||||
@@ -5229,8 +5245,15 @@ void cbm_run_kotlin_lsp(CBMArena *arena, CBMFileResult *result, const char *sour
|
||||
kotlin_lsp_init(&ctx, arena, use_source, use_source_len, ®istry, module_qn, module_qn,
|
||||
project_name, /*rel_path=*/NULL, &result->resolved_calls);
|
||||
ctx.synthetic_calls = &result->calls;
|
||||
/* The registry's lookups run while it fills and after: its types are found by
|
||||
* name as they come (tail_arena), and all of it once it is indexed. */
|
||||
CBMArena index_arena;
|
||||
cbm_arena_init_lazy(&index_arena, KT_INDEX_ARENA_BLOCK);
|
||||
ctx.overlay_index_arena = &index_arena;
|
||||
registry.tail_arena = &index_arena;
|
||||
|
||||
kotlin_lsp_process_file(&ctx, use_root);
|
||||
cbm_arena_destroy(&index_arena);
|
||||
|
||||
/* Inject kotlin.Any universal-method nodes (toString/equals/hashCode) so the
|
||||
* lsp_kt_any fallback emitted above has a target node to form a CALLS edge. */
|
||||
@@ -5584,6 +5607,7 @@ void cbm_run_kotlin_lsp_cross(CBMArena *arena, const char *source, int source_le
|
||||
kotlin_lsp_init(&ctx, arena, source, source_len, &overlay, module_qn ? module_qn : "",
|
||||
module_qn ? module_qn : "", project_name, /*rel_path=*/NULL, out);
|
||||
ctx.overlay_index_arena = &idx_arena;
|
||||
overlay.tail_arena = &idx_arena;
|
||||
|
||||
/* Unique short type name -> registered QN, for receiver annotations that
|
||||
* name a type defined in another file without an import. Two types
|
||||
|
||||
@@ -48,6 +48,7 @@
|
||||
#ifndef CBM_LSP_KOTLIN_LSP_H
|
||||
#define CBM_LSP_KOTLIN_LSP_H
|
||||
|
||||
#include "lsp_name_rows.h"
|
||||
#include "type_rep.h"
|
||||
#include "scope.h"
|
||||
#include "type_registry.h"
|
||||
@@ -97,6 +98,7 @@ typedef struct KotlinLSPContext {
|
||||
CBMKotlinUseKind *import_kinds;
|
||||
int import_count;
|
||||
int import_cap;
|
||||
CBMNameRows import_rows; /* import_locals by name, rows in table order */
|
||||
|
||||
/* Current declaration context. */
|
||||
const char *enclosing_func_qn; /* function we are resolving inside */
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
/*
|
||||
* lsp_name_rows.h — rows of a per-file table, found by name.
|
||||
*
|
||||
* A file's import table is searched once per identifier, and a scan of it makes that
|
||||
* imports x identifiers: a generated module with thousands of imports took seconds in
|
||||
* each pass. These chains answer with the same rows in the same order.
|
||||
*
|
||||
* cbm_name_rows_first/next index a table of names on the first question, and only a
|
||||
* table long enough for that to matter; a short one, which is nearly all of them, is
|
||||
* still scanned and allocates nothing. Rows appended later join on the next question.
|
||||
* A caller that shrinks or rewrites its table zeroes the CBMNameRows.
|
||||
*
|
||||
* cbm_name_rows_add/lookup are the same chains built by hand, for a key that also has a
|
||||
* scope. Memory comes from the caller's arena.
|
||||
*/
|
||||
#ifndef CBM_LSP_NAME_ROWS_H
|
||||
#define CBM_LSP_NAME_ROWS_H
|
||||
|
||||
#include "../arena.h"
|
||||
#include "lsp_work.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
enum { CBM_NAME_ROWS_MIN = 16, CBM_NAME_ROWS_FIRST_SLOTS = 32 };
|
||||
|
||||
typedef struct {
|
||||
uint64_t hash;
|
||||
const void *scope;
|
||||
const char *name;
|
||||
int first, last; /* row + 1; 0 marks an unused slot */
|
||||
} CBMNameRowSlot;
|
||||
|
||||
typedef struct {
|
||||
CBMNameRowSlot *slots;
|
||||
uint32_t capacity, used;
|
||||
int *next; /* row -> the next row under its key, or -1 */
|
||||
int next_capacity;
|
||||
int synced; /* table rows already chained */
|
||||
bool failed;
|
||||
bool chained; /* cbm_name_rows_add callers: every row is in */
|
||||
} CBMNameRows;
|
||||
|
||||
static inline uint64_t cbm_name_rows_hash(const void *scope, const char *name) {
|
||||
uint64_t hash = ((uint64_t)(uintptr_t)scope >> 3) * UINT64_C(0x9E3779B97F4A7C15);
|
||||
for (const char *at = name; at && *at; at++)
|
||||
hash = (hash ^ (unsigned char)*at) * UINT64_C(0x100000001B3);
|
||||
return hash ^ (hash >> 32);
|
||||
}
|
||||
|
||||
static inline CBMNameRowSlot *cbm_name_rows_slot(const CBMNameRows *rows, uint64_t hash,
|
||||
const void *scope, const char *name) {
|
||||
uint32_t mask = rows->capacity - 1;
|
||||
for (uint32_t at = (uint32_t)hash & mask;; at = (at + 1) & mask) {
|
||||
CBMNameRowSlot *slot = &rows->slots[at];
|
||||
if (!slot->first ||
|
||||
(slot->hash == hash && slot->scope == scope &&
|
||||
(slot->name == name || (slot->name && name && strcmp(slot->name, name) == 0)))) {
|
||||
return slot;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Rows go in ascending order. false when memory ran out. */
|
||||
static inline bool cbm_name_rows_add(CBMArena *arena, CBMNameRows *rows, const void *scope,
|
||||
const char *name, int row) {
|
||||
if (!arena)
|
||||
return false;
|
||||
if (row >= rows->next_capacity) {
|
||||
int capacity = rows->next_capacity ? rows->next_capacity * 2 : CBM_NAME_ROWS_FIRST_SLOTS;
|
||||
while (capacity <= row)
|
||||
capacity *= 2;
|
||||
int *next = (int *)cbm_arena_alloc(arena, (size_t)capacity * sizeof(*next));
|
||||
if (!next)
|
||||
return false;
|
||||
if (rows->next_capacity)
|
||||
memcpy(next, rows->next, (size_t)rows->next_capacity * sizeof(*next));
|
||||
rows->next = next;
|
||||
rows->next_capacity = capacity;
|
||||
}
|
||||
if (rows->used * 2 >= rows->capacity) {
|
||||
uint32_t capacity = rows->capacity ? rows->capacity * 2 : CBM_NAME_ROWS_FIRST_SLOTS;
|
||||
CBMNameRowSlot *slots =
|
||||
(CBMNameRowSlot *)cbm_arena_alloc(arena, (size_t)capacity * sizeof(*slots));
|
||||
if (!slots)
|
||||
return false;
|
||||
memset(slots, 0, (size_t)capacity * sizeof(*slots));
|
||||
CBMNameRows grown = *rows;
|
||||
grown.slots = slots;
|
||||
grown.capacity = capacity;
|
||||
for (uint32_t i = 0; i < rows->capacity; i++) {
|
||||
const CBMNameRowSlot *old = &rows->slots[i];
|
||||
if (old->first)
|
||||
*cbm_name_rows_slot(&grown, old->hash, old->scope, old->name) = *old;
|
||||
}
|
||||
*rows = grown;
|
||||
}
|
||||
uint64_t hash = cbm_name_rows_hash(scope, name);
|
||||
CBMNameRowSlot *slot = cbm_name_rows_slot(rows, hash, scope, name);
|
||||
rows->next[row] = -1;
|
||||
if (slot->first) {
|
||||
rows->next[slot->last - 1] = row;
|
||||
} else {
|
||||
*slot = (CBMNameRowSlot){.hash = hash, .scope = scope, .name = name, .first = row + 1};
|
||||
rows->used++;
|
||||
}
|
||||
slot->last = row + 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline int cbm_name_rows_lookup(const CBMNameRows *rows, const void *scope,
|
||||
const char *name) {
|
||||
if (!rows->capacity)
|
||||
return -1;
|
||||
return cbm_name_rows_slot(rows, cbm_name_rows_hash(scope, name), scope, name)->first - 1;
|
||||
}
|
||||
|
||||
static inline bool cbm_name_rows_chained(const CBMNameRows *rows, int count) {
|
||||
return !rows->failed && count >= CBM_NAME_ROWS_MIN && rows->synced == count;
|
||||
}
|
||||
|
||||
static inline int cbm_name_rows_scan(const char *const *names, int from, int count,
|
||||
const char *name) {
|
||||
for (int i = from; i < count; i++) {
|
||||
CBM_LSP_WORK(1);
|
||||
if (names[i] && strcmp(names[i], name) == 0)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* The first row of names[0..count) equal to name, or -1. */
|
||||
static inline int cbm_name_rows_first(CBMNameRows *rows, CBMArena *arena, const char *const *names,
|
||||
int count, const char *name) {
|
||||
if (count >= CBM_NAME_ROWS_MIN && !rows->failed) {
|
||||
for (; rows->synced < count; rows->synced++) {
|
||||
int row = rows->synced;
|
||||
if (names[row] && !cbm_name_rows_add(arena, rows, NULL, names[row], row)) {
|
||||
rows->failed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!cbm_name_rows_chained(rows, count))
|
||||
return cbm_name_rows_scan(names, 0, count, name);
|
||||
CBM_LSP_WORK(1);
|
||||
return cbm_name_rows_lookup(rows, NULL, name);
|
||||
}
|
||||
|
||||
/* The next such row after row, or -1. */
|
||||
static inline int cbm_name_rows_next(const CBMNameRows *rows, const char *const *names, int count,
|
||||
const char *name, int row) {
|
||||
if (!cbm_name_rows_chained(rows, count))
|
||||
return cbm_name_rows_scan(names, row + 1, count, name);
|
||||
CBM_LSP_WORK(1);
|
||||
return rows->next[row];
|
||||
}
|
||||
|
||||
#endif /* CBM_LSP_NAME_ROWS_H */
|
||||
@@ -184,8 +184,24 @@ void php_lsp_add_use(PHPLSPContext *ctx, const char *local_name, const char *tar
|
||||
ctx->use_count++;
|
||||
}
|
||||
|
||||
/* The registry's free functions of this short name, in registration order, through
|
||||
* its short-name index and the rows behind it. An indexed registry whose short-name
|
||||
* index could not be built is walked in full, as the scan this replaces was (the
|
||||
* caller re-checks each row). */
|
||||
static void php_free_funcs_by_name(const CBMTypeRegistry *reg, const char *name,
|
||||
CBMFreeFuncIter *it) {
|
||||
cbm_registry_free_funcs_by_short_name(reg, name, it);
|
||||
if (it->chain_idx < 0 && !reg->ffunc_short_buckets)
|
||||
it->tail_i = 0;
|
||||
}
|
||||
|
||||
/* The uses with this local name, in table order (lsp_name_rows.h): a scan per
|
||||
* name made a file of thousands of `use` declarations quadratic. */
|
||||
static const char *find_use(PHPLSPContext *ctx, const char *local_name, int kind) {
|
||||
for (int i = 0; i < ctx->use_count; i++) {
|
||||
for (int i = cbm_name_rows_first(&ctx->use_rows, ctx->arena, ctx->use_local_names,
|
||||
ctx->use_count, local_name);
|
||||
i >= 0; i = cbm_name_rows_next(&ctx->use_rows, ctx->use_local_names, ctx->use_count,
|
||||
local_name, i)) {
|
||||
if ((int)ctx->use_kinds[i] != kind)
|
||||
continue;
|
||||
if (strcmp(ctx->use_local_names[i], local_name) == 0)
|
||||
@@ -325,7 +341,12 @@ static const CBMRegisteredType *lookup_type_with_project(PHPLSPContext *ctx, con
|
||||
|
||||
const CBMRegisteredType *best = NULL;
|
||||
int best_score = -1;
|
||||
for (int i = 0; ctx->registry && i < ctx->registry->type_count; i++) {
|
||||
/* The registry's own types of this short name, in registration order. */
|
||||
CBMTypeShortIter named = {0};
|
||||
if (ctx->registry)
|
||||
cbm_registry_types_by_short_name(ctx->registry, short_name, &named);
|
||||
for (int i = -1; ctx->registry && (i = cbm_type_short_iter_next(&named)) >= 0;) {
|
||||
CBM_LSP_WORK(1);
|
||||
const CBMRegisteredType *cand = &ctx->registry->types[i];
|
||||
if (!cand->short_name || strcmp(cand->short_name, short_name) != 0)
|
||||
continue;
|
||||
@@ -1202,7 +1223,10 @@ static const CBMType *eval_function_call_type(PHPLSPContext *ctx, TSNode call_no
|
||||
}
|
||||
if (!f && ctx->registry) {
|
||||
/* Walk the symbol table by short_name as a last resort. */
|
||||
for (int i = 0; i < ctx->registry->func_count; i++) {
|
||||
CBMFreeFuncIter named;
|
||||
php_free_funcs_by_name(ctx->registry, name, &named);
|
||||
for (int i = -1; (i = cbm_free_func_iter_next(&named)) >= 0;) {
|
||||
CBM_LSP_WORK(1);
|
||||
const CBMRegisteredFunc *cand = &ctx->registry->funcs[i];
|
||||
if (cand->receiver_type)
|
||||
continue;
|
||||
@@ -1696,7 +1720,11 @@ static void resolve_function_call(PHPLSPContext *ctx, TSNode call, CBMResolvedKi
|
||||
* file's project (i.e. with the longest module-prefix overlap). */
|
||||
const CBMRegisteredFunc *best = NULL;
|
||||
int best_score = -1;
|
||||
for (int i = 0; ctx->registry && i < ctx->registry->func_count; i++) {
|
||||
CBMFreeFuncIter named = {0};
|
||||
if (ctx->registry)
|
||||
php_free_funcs_by_name(ctx->registry, name, &named);
|
||||
for (int i = -1; ctx->registry && (i = cbm_free_func_iter_next(&named)) >= 0;) {
|
||||
CBM_LSP_WORK(1);
|
||||
const CBMRegisteredFunc *cand = &ctx->registry->funcs[i];
|
||||
if (cand->receiver_type)
|
||||
continue;
|
||||
@@ -4034,6 +4062,22 @@ static void process_trait_use(PHPLSPContext *ctx, CBMTypeRegistry *reg, const ch
|
||||
}
|
||||
}
|
||||
|
||||
/* Where a scan for the first function of reg itself with this receiver and name
|
||||
* can start: the lowest row the method iterator yields (reg's own rows only: its
|
||||
* index chain, in whichever order that was built, then the rows behind it; every
|
||||
* row when unindexed). reg->func_count when there is none. */
|
||||
static int php_first_method_row(const CBMTypeRegistry *reg, const char *receiver,
|
||||
const char *name) {
|
||||
CBMMethodIter it;
|
||||
cbm_registry_methods(reg, receiver, name, &it);
|
||||
int lowest = reg->func_count;
|
||||
for (int row = cbm_method_iter_next(&it); row >= 0; row = cbm_method_iter_next(&it)) {
|
||||
if (row < lowest)
|
||||
lowest = row;
|
||||
}
|
||||
return lowest;
|
||||
}
|
||||
|
||||
static void process_class_for_fields(PHPLSPContext *ctx, CBMTypeRegistry *reg, TSNode class_node,
|
||||
php_class_field_table_t *tab) {
|
||||
const char *cqn = class_qn_for_node(ctx, class_node);
|
||||
@@ -4215,7 +4259,10 @@ static void process_class_for_fields(PHPLSPContext *ctx, CBMTypeRegistry *reg, T
|
||||
ctx->enclosing_class_qn = saved_class;
|
||||
ctx->enclosing_parent_qn = saved_parent;
|
||||
if (parsed && parsed->kind != CBM_TYPE_UNKNOWN) {
|
||||
for (int fi = 0; fi < reg->func_count; fi++) {
|
||||
/* The loop patches the first registration of (cqn, mn): its
|
||||
* method index answers in registration order; start there. */
|
||||
CBM_LSP_WORK(1);
|
||||
for (int fi = php_first_method_row(reg, cqn, mn); fi < reg->func_count; fi++) {
|
||||
CBMRegisteredFunc *rf = ®->funcs[fi];
|
||||
if (!rf->receiver_type || !rf->short_name)
|
||||
continue;
|
||||
@@ -4292,7 +4339,9 @@ static void process_class_for_fields(PHPLSPContext *ctx, CBMTypeRegistry *reg, T
|
||||
if (ret_t && ret_t->kind != CBM_TYPE_UNKNOWN) {
|
||||
/* Find the registered method by receiver_type=cqn,
|
||||
* short_name=mn and patch its signature. */
|
||||
for (int fi = 0; fi < reg->func_count; fi++) {
|
||||
CBM_LSP_WORK(1);
|
||||
for (int fi = php_first_method_row(reg, cqn, mn); fi < reg->func_count;
|
||||
fi++) {
|
||||
CBMRegisteredFunc *rf = ®->funcs[fi];
|
||||
if (!rf->receiver_type || !rf->short_name)
|
||||
continue;
|
||||
@@ -4540,6 +4589,7 @@ void cbm_run_php_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
|
||||
/* Reset namespace/use state — process_file will re-collect. */
|
||||
ctx.current_namespace_qn = "";
|
||||
ctx.use_count = 0;
|
||||
ctx.use_rows = (CBMNameRows){0};
|
||||
}
|
||||
|
||||
/* Trait flattening registered methods after the index was built; lookups
|
||||
@@ -4875,6 +4925,7 @@ void cbm_run_php_lsp_cross_with_bindings(CBMArena *arena, const char *source, in
|
||||
ctx.current_namespace_qn = "";
|
||||
/* Reset to caller-supplied uses only — process_file re-adds AST uses. */
|
||||
ctx.use_count = supplied_use_count;
|
||||
ctx.use_rows = (CBMNameRows){0};
|
||||
}
|
||||
|
||||
php_lsp_process_file(&ctx, root);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef CBM_LSP_PHP_LSP_H
|
||||
#define CBM_LSP_PHP_LSP_H
|
||||
|
||||
#include "lsp_name_rows.h"
|
||||
#include "type_rep.h"
|
||||
#include "scope.h"
|
||||
#include "type_registry.h"
|
||||
@@ -36,6 +37,7 @@ typedef struct {
|
||||
enum { CBM_PHP_USE_CLASS = 0, CBM_PHP_USE_FUNCTION, CBM_PHP_USE_CONST } *use_kinds;
|
||||
int use_count;
|
||||
int use_cap;
|
||||
CBMNameRows use_rows; /* use_local_names by name; zeroed whenever use_count is reset */
|
||||
|
||||
/* Cross-file mode: dotted targets of the file's class `use` declarations
|
||||
* that the pipeline's import map left unresolved (vendor / unmapped
|
||||
|
||||
+253
-77
@@ -394,37 +394,55 @@ void py_lsp_init(PyLSPContext *ctx, CBMArena *arena, const char *source, int sou
|
||||
py_lsp_init_for_stage(ctx, arena, source, source_len, registry, module_qn, out, "context");
|
||||
}
|
||||
|
||||
enum { PY_IMPORT_FIRST_CAPACITY = 16, PY_IMPORT_JOIN_BLOCK = 4096 };
|
||||
|
||||
void py_lsp_add_import(PyLSPContext *ctx, const char *local_name, const char *module_qn) {
|
||||
if (!ctx || !local_name || !module_qn)
|
||||
return;
|
||||
|
||||
/* The arrays double when full: copied on every import, a module of thousands
|
||||
* of imports was quadratic. Each name and QN array keeps a slot for its NULL. */
|
||||
int new_count = ctx->import_count + 1;
|
||||
const char **names =
|
||||
(const char **)cbm_arena_alloc(ctx->arena, (size_t)(new_count + 1) * sizeof(const char *));
|
||||
const char **qns =
|
||||
(const char **)cbm_arena_alloc(ctx->arena, (size_t)(new_count + 1) * sizeof(const char *));
|
||||
unsigned char *kinds =
|
||||
(unsigned char *)cbm_arena_alloc(ctx->arena, (size_t)new_count * sizeof(unsigned char));
|
||||
if (!names || !qns || !kinds)
|
||||
return;
|
||||
|
||||
for (int i = 0; i < ctx->import_count; i++) {
|
||||
names[i] = ctx->import_local_names[i];
|
||||
qns[i] = ctx->import_module_qns[i];
|
||||
kinds[i] = ctx->import_kinds ? ctx->import_kinds[i] : 0;
|
||||
if (new_count > ctx->import_capacity) {
|
||||
int capacity = ctx->import_capacity ? ctx->import_capacity * 2 : PY_IMPORT_FIRST_CAPACITY;
|
||||
const char **names = (const char **)cbm_arena_alloc(ctx->arena, (size_t)(capacity + 1) *
|
||||
sizeof(const char *));
|
||||
const char **qns = (const char **)cbm_arena_alloc(ctx->arena, (size_t)(capacity + 1) *
|
||||
sizeof(const char *));
|
||||
unsigned char *kinds =
|
||||
(unsigned char *)cbm_arena_alloc(ctx->arena, (size_t)capacity * sizeof(unsigned char));
|
||||
if (!names || !qns || !kinds)
|
||||
return;
|
||||
for (int i = 0; i < ctx->import_count; i++) {
|
||||
CBM_LSP_WORK(1);
|
||||
names[i] = ctx->import_local_names[i];
|
||||
qns[i] = ctx->import_module_qns[i];
|
||||
kinds[i] = ctx->import_kinds ? ctx->import_kinds[i] : 0;
|
||||
}
|
||||
ctx->import_local_names = names;
|
||||
ctx->import_module_qns = qns;
|
||||
ctx->import_kinds = kinds;
|
||||
ctx->import_capacity = capacity;
|
||||
}
|
||||
names[ctx->import_count] = cbm_arena_strdup(ctx->arena, local_name);
|
||||
qns[ctx->import_count] = cbm_arena_strdup(ctx->arena, module_qn);
|
||||
kinds[ctx->import_count] = 0;
|
||||
names[new_count] = NULL;
|
||||
qns[new_count] = NULL;
|
||||
|
||||
ctx->import_local_names = names;
|
||||
ctx->import_module_qns = qns;
|
||||
ctx->import_kinds = kinds;
|
||||
ctx->import_local_names[ctx->import_count] = cbm_arena_strdup(ctx->arena, local_name);
|
||||
ctx->import_module_qns[ctx->import_count] = cbm_arena_strdup(ctx->arena, module_qn);
|
||||
ctx->import_kinds[ctx->import_count] = 0;
|
||||
ctx->import_local_names[new_count] = NULL;
|
||||
ctx->import_module_qns[new_count] = NULL;
|
||||
ctx->import_count = new_count;
|
||||
}
|
||||
|
||||
/* The import rows with this local name, in table order (lsp_name_rows.h). */
|
||||
static int py_import_row_first(PyLSPContext *ctx, const char *local) {
|
||||
return cbm_name_rows_first(&ctx->import_rows, ctx->arena, ctx->import_local_names,
|
||||
ctx->import_count, local);
|
||||
}
|
||||
|
||||
static int py_import_row_next(const PyLSPContext *ctx, const char *local, int row) {
|
||||
return cbm_name_rows_next(&ctx->import_rows, ctx->import_local_names, ctx->import_count, local,
|
||||
row);
|
||||
}
|
||||
|
||||
/* Determine whether this import is an `import X` style binding (binds the
|
||||
* module itself) or a `from X import Y` style binding (binds Y, an
|
||||
* attribute of module X). Heuristic: if module_qn ends in `.<local_name>`
|
||||
@@ -575,64 +593,193 @@ static const char *py_canonical_from_import_qn(PyLSPContext *ctx, TSNode stateme
|
||||
* binding for the local name. Python permits later imports to rebind the same
|
||||
* local; selecting the first would turn conflicting runtime identities into
|
||||
* false semantic proof. */
|
||||
static void py_import_match_item(PyLSPContext *ctx, TSNode stmt, bool from_import, TSNode item,
|
||||
const char *local, const char *qn, PyImportSyntaxMatch *match) {
|
||||
const char *kind = ts_node_type(item);
|
||||
if (from_import) {
|
||||
if (strcmp(kind, "aliased_import") == 0) {
|
||||
TSNode name = ts_node_child_by_field_name(item, "name", 4);
|
||||
TSNode alias = ts_node_child_by_field_name(item, "alias", 5);
|
||||
if (py_import_node_text_equals(ctx, alias, local)) {
|
||||
const char *canonical = py_canonical_from_import_qn(ctx, stmt, name, qn);
|
||||
py_import_syntax_match_add(match,
|
||||
canonical ? PY_FROM_IMPORT : PY_IMPORT_UNCLASSIFIED,
|
||||
canonical ? canonical : qn);
|
||||
}
|
||||
} else if ((strcmp(kind, "identifier") == 0 || strcmp(kind, "dotted_name") == 0) &&
|
||||
py_import_node_text_equals(ctx, item, local)) {
|
||||
const char *canonical = py_canonical_from_import_qn(ctx, stmt, item, qn);
|
||||
py_import_syntax_match_add(match, canonical ? PY_FROM_IMPORT : PY_IMPORT_UNCLASSIFIED,
|
||||
canonical ? canonical : qn);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (strcmp(kind, "aliased_import") == 0) {
|
||||
TSNode name = ts_node_child_by_field_name(item, "name", 4);
|
||||
TSNode alias = ts_node_child_by_field_name(item, "alias", 5);
|
||||
if (py_import_node_text_equals(ctx, alias, local)) {
|
||||
char *imported_path = py_import_node_text_dup(ctx, name);
|
||||
PyDirectImportKind matched_kind =
|
||||
imported_path && py_qn_has_boundary_suffix(qn, imported_path)
|
||||
? PY_DIRECT_IMPORT_ALIASED
|
||||
: PY_IMPORT_UNCLASSIFIED;
|
||||
py_import_syntax_match_add(match, matched_kind, qn);
|
||||
}
|
||||
} else if ((strcmp(kind, "dotted_name") == 0 || strcmp(kind, "identifier") == 0) &&
|
||||
py_import_node_root_equals(ctx, item, local)) {
|
||||
char *imported_path = py_import_node_text_dup(ctx, item);
|
||||
PyDirectImportKind matched_kind =
|
||||
imported_path && py_qn_has_boundary_suffix(qn, imported_path)
|
||||
? PY_DIRECT_IMPORT_UNALIASED
|
||||
: PY_IMPORT_UNCLASSIFIED;
|
||||
py_import_syntax_match_add(match, matched_kind, qn);
|
||||
}
|
||||
}
|
||||
|
||||
static void py_import_match_statement(PyLSPContext *ctx, TSNode stmt, const char *local,
|
||||
const char *qn, PyImportSyntaxMatch *match) {
|
||||
if (!ctx || ts_node_is_null(stmt) || !local || !qn || !match)
|
||||
return;
|
||||
const char *stmt_kind = ts_node_type(stmt);
|
||||
if (strcmp(stmt_kind, "import_from_statement") == 0) {
|
||||
TSNode module = py_from_import_module_node(stmt);
|
||||
uint32_t import_count = ts_node_named_child_count(stmt);
|
||||
for (uint32_t j = 0; j < import_count; j++) {
|
||||
TSNode item = ts_node_named_child(stmt, j);
|
||||
if (!ts_node_is_null(module) && ts_node_eq(item, module))
|
||||
continue;
|
||||
const char *kind = ts_node_type(item);
|
||||
if (strcmp(kind, "aliased_import") == 0) {
|
||||
TSNode name = ts_node_child_by_field_name(item, "name", 4);
|
||||
TSNode alias = ts_node_child_by_field_name(item, "alias", 5);
|
||||
if (py_import_node_text_equals(ctx, alias, local)) {
|
||||
const char *canonical = py_canonical_from_import_qn(ctx, stmt, name, qn);
|
||||
py_import_syntax_match_add(match,
|
||||
canonical ? PY_FROM_IMPORT : PY_IMPORT_UNCLASSIFIED,
|
||||
canonical ? canonical : qn);
|
||||
}
|
||||
} else if ((strcmp(kind, "identifier") == 0 || strcmp(kind, "dotted_name") == 0) &&
|
||||
py_import_node_text_equals(ctx, item, local)) {
|
||||
const char *canonical = py_canonical_from_import_qn(ctx, stmt, item, qn);
|
||||
py_import_syntax_match_add(match,
|
||||
canonical ? PY_FROM_IMPORT : PY_IMPORT_UNCLASSIFIED,
|
||||
canonical ? canonical : qn);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (strcmp(stmt_kind, "import_statement") != 0)
|
||||
bool from_import = strcmp(stmt_kind, "import_from_statement") == 0;
|
||||
if (!from_import && strcmp(stmt_kind, "import_statement") != 0)
|
||||
return;
|
||||
TSNode module = from_import ? py_from_import_module_node(stmt) : (TSNode){0};
|
||||
uint32_t import_count = ts_node_named_child_count(stmt);
|
||||
for (uint32_t j = 0; j < import_count; j++) {
|
||||
CBM_LSP_WORK(1);
|
||||
TSNode item = ts_node_named_child(stmt, j);
|
||||
const char *kind = ts_node_type(item);
|
||||
if (strcmp(kind, "aliased_import") == 0) {
|
||||
TSNode name = ts_node_child_by_field_name(item, "name", 4);
|
||||
TSNode alias = ts_node_child_by_field_name(item, "alias", 5);
|
||||
if (py_import_node_text_equals(ctx, alias, local)) {
|
||||
char *imported_path = py_import_node_text_dup(ctx, name);
|
||||
PyDirectImportKind matched_kind =
|
||||
imported_path && py_qn_has_boundary_suffix(qn, imported_path)
|
||||
? PY_DIRECT_IMPORT_ALIASED
|
||||
: PY_IMPORT_UNCLASSIFIED;
|
||||
py_import_syntax_match_add(match, matched_kind, qn);
|
||||
}
|
||||
} else if ((strcmp(kind, "dotted_name") == 0 || strcmp(kind, "identifier") == 0) &&
|
||||
py_import_node_root_equals(ctx, item, local)) {
|
||||
char *imported_path = py_import_node_text_dup(ctx, item);
|
||||
PyDirectImportKind matched_kind =
|
||||
imported_path && py_qn_has_boundary_suffix(qn, imported_path)
|
||||
? PY_DIRECT_IMPORT_UNALIASED
|
||||
: PY_IMPORT_UNCLASSIFIED;
|
||||
py_import_syntax_match_add(match, matched_kind, qn);
|
||||
if (from_import && !ts_node_is_null(module) && ts_node_eq(item, module))
|
||||
continue;
|
||||
py_import_match_item(ctx, stmt, from_import, item, local, qn, match);
|
||||
}
|
||||
}
|
||||
|
||||
/* ── The import statements' items, by the names they bind ─────────────
|
||||
* Each import asked every import statement of the module, and each item of a
|
||||
* statement asked every import, which was imports x items. The items are
|
||||
* filed once per walk under exactly the names py_import_match_item matches
|
||||
* them on, in source order, so only those items are matched. Running out of
|
||||
* memory sets `failed`, and every question is answered by the scan again. */
|
||||
typedef struct {
|
||||
TSNode statement;
|
||||
TSNode item; /* null on a statement's own row */
|
||||
bool from_import;
|
||||
} PyImportItem;
|
||||
|
||||
struct PyImportJoin {
|
||||
CBMArena *arena;
|
||||
CBMNameRows by_name; /* (statement, name) and (NULL, name) -> the items binding name */
|
||||
PyImportItem *items;
|
||||
int item_count;
|
||||
int item_capacity;
|
||||
bool failed;
|
||||
};
|
||||
|
||||
/* Files one name an item binds under its statement and, for a statement of
|
||||
* the module's own list, under NULL. A NULL name files the statement's own row. */
|
||||
static bool py_import_join_file(struct PyImportJoin *join, PyImportItem item, const char *name,
|
||||
bool module_list) {
|
||||
for (int under_statement = module_list ? 0 : 1; under_statement < 2; under_statement++) {
|
||||
if (join->item_count == join->item_capacity) {
|
||||
int capacity = join->item_capacity ? join->item_capacity * 2 : PY_IMPORT_FIRST_CAPACITY;
|
||||
PyImportItem *items =
|
||||
(PyImportItem *)cbm_arena_alloc(join->arena, (size_t)capacity * sizeof(*items));
|
||||
if (!items)
|
||||
return false;
|
||||
if (join->item_count)
|
||||
memcpy(items, join->items, (size_t)join->item_count * sizeof(*items));
|
||||
join->items = items;
|
||||
join->item_capacity = capacity;
|
||||
}
|
||||
int row = join->item_count;
|
||||
join->items[row] = item;
|
||||
const void *scope = under_statement ? item.statement.id : NULL;
|
||||
if (!cbm_name_rows_add(join->arena, &join->by_name, scope, name, row))
|
||||
return false;
|
||||
join->item_count++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/* The name an item binds, as py_import_match_item reads it; *name stays NULL
|
||||
* when the item binds none. False only when memory ran out. */
|
||||
static bool py_import_join_item_name(PyLSPContext *ctx, struct PyImportJoin *join, TSNode node,
|
||||
char **name) {
|
||||
*name = NULL;
|
||||
if (ts_node_is_null(node))
|
||||
return true;
|
||||
uint32_t start = ts_node_start_byte(node);
|
||||
uint32_t end = ts_node_end_byte(node);
|
||||
if (end < start || (int)end > ctx->source_len)
|
||||
return true;
|
||||
*name = cbm_arena_strndup(join->arena, ctx->source + start, (size_t)(end - start));
|
||||
return *name != NULL;
|
||||
}
|
||||
|
||||
static bool py_import_join_items(PyLSPContext *ctx, struct PyImportJoin *join, TSNode stmt,
|
||||
bool from_import, bool module_list) {
|
||||
TSNode module = from_import ? py_from_import_module_node(stmt) : (TSNode){0};
|
||||
PyKids kids = py_kids(join->arena, stmt);
|
||||
for (uint32_t i = 0; i < kids.n; i++) {
|
||||
TSNode item = py_kid(&kids, i);
|
||||
if (from_import && !ts_node_is_null(module) && ts_node_eq(item, module))
|
||||
continue;
|
||||
const char *kind = ts_node_type(item);
|
||||
bool aliased = strcmp(kind, "aliased_import") == 0;
|
||||
if (!aliased && strcmp(kind, "identifier") != 0 && strcmp(kind, "dotted_name") != 0)
|
||||
continue;
|
||||
char *name = NULL;
|
||||
TSNode named = aliased ? ts_node_child_by_field_name(item, "alias", 5) : item;
|
||||
if (!py_import_join_item_name(ctx, join, named, &name))
|
||||
return false;
|
||||
if (!name)
|
||||
continue;
|
||||
PyImportItem row = {.statement = stmt, .item = item, .from_import = from_import};
|
||||
/* `import a.b.c` binds a, and answers for a, a.b and a.b.c. */
|
||||
if (!aliased && !from_import) {
|
||||
for (char *dot = strchr(name, '.'); dot; dot = strchr(dot + 1, '.')) {
|
||||
char *prefix = cbm_arena_strndup(join->arena, name, (size_t)(dot - name));
|
||||
if (!prefix || !py_import_join_file(join, row, prefix, module_list))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (!py_import_join_file(join, row, name, module_list))
|
||||
return false;
|
||||
}
|
||||
PyImportItem own = {.statement = stmt, .from_import = from_import};
|
||||
return py_import_join_file(join, own, NULL, false);
|
||||
}
|
||||
|
||||
/* True when the join can answer for stmt's items: filed now or by an earlier question. */
|
||||
static bool py_import_join_statement(PyLSPContext *ctx, TSNode stmt, bool module_list) {
|
||||
struct PyImportJoin *join = ctx->import_join;
|
||||
if (!join || join->failed)
|
||||
return false;
|
||||
if (cbm_name_rows_lookup(&join->by_name, stmt.id, NULL) >= 0)
|
||||
return true;
|
||||
const char *stmt_kind = ts_node_type(stmt);
|
||||
bool from_import = strcmp(stmt_kind, "import_from_statement") == 0;
|
||||
if (!from_import && strcmp(stmt_kind, "import_statement") != 0)
|
||||
return false;
|
||||
if (!py_import_join_items(ctx, join, stmt, from_import, module_list)) {
|
||||
join->failed = true;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/* The items filed under (scope, local), in source order. The answer depends on
|
||||
* the first match and on whether there is a second one, so the second ends it. */
|
||||
static void py_import_join_match(PyLSPContext *ctx, const void *scope, const char *local,
|
||||
const char *qn, PyImportSyntaxMatch *match) {
|
||||
const struct PyImportJoin *join = ctx->import_join;
|
||||
CBM_LSP_WORK(1);
|
||||
for (int row = cbm_name_rows_lookup(&join->by_name, scope, local); row >= 0 && match->count < 2;
|
||||
row = join->by_name.next[row]) {
|
||||
CBM_LSP_WORK(1);
|
||||
const PyImportItem *item = &join->items[row];
|
||||
py_import_match_item(ctx, item->statement, item->from_import, item->item, local, qn, match);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -654,14 +801,18 @@ static PyDirectImportKind py_import_kind_from_statement(PyLSPContext *ctx, TSNod
|
||||
if (!ctx || !local || !qn || !qn_io || ts_node_is_null(stmt))
|
||||
return PY_DIRECT_IMPORT_UNKNOWN;
|
||||
PyImportSyntaxMatch match = {0};
|
||||
py_import_match_statement(ctx, stmt, local, qn, &match);
|
||||
if (py_import_join_statement(ctx, stmt, false))
|
||||
py_import_join_match(ctx, stmt.id, local, qn, &match);
|
||||
else
|
||||
py_import_match_statement(ctx, stmt, local, qn, &match);
|
||||
return py_import_match_result(&match, qn_io);
|
||||
}
|
||||
|
||||
/* stmts: the root's import statements, collected once per root by the caller
|
||||
* (#1527) -- only they can match, and rescanning every top-level statement
|
||||
* once per import made classification O(imports x module size). */
|
||||
static PyDirectImportKind py_import_kind_from_ast(PyLSPContext *ctx, bool have_root,
|
||||
* once per import made classification O(imports x module size). `joined`:
|
||||
* their items are filed by name, so only the items binding `local` are asked. */
|
||||
static PyDirectImportKind py_import_kind_from_ast(PyLSPContext *ctx, bool have_root, bool joined,
|
||||
const TSNode *stmts, uint32_t stmt_count,
|
||||
const char *local, const char **qn_io) {
|
||||
const char *qn = qn_io ? *qn_io : NULL;
|
||||
@@ -669,6 +820,10 @@ static PyDirectImportKind py_import_kind_from_ast(PyLSPContext *ctx, bool have_r
|
||||
return PY_DIRECT_IMPORT_UNKNOWN;
|
||||
|
||||
PyImportSyntaxMatch match = {0};
|
||||
if (joined) {
|
||||
py_import_join_match(ctx, NULL, local, qn, &match);
|
||||
return py_import_match_result(&match, qn_io);
|
||||
}
|
||||
for (uint32_t i = 0; i < stmt_count; i++) {
|
||||
py_import_match_statement(ctx, stmts[i], local, qn, &match);
|
||||
}
|
||||
@@ -736,13 +891,18 @@ static void py_classify_imports_for_root(PyLSPContext *ctx, TSNode root) {
|
||||
stmts[stmt_count++] = c;
|
||||
}
|
||||
}
|
||||
/* A statement the join could not file leaves every import to the scan. */
|
||||
bool joined =
|
||||
have_root && ctx->import_count > 0 && ctx->import_join && !ctx->import_join->failed;
|
||||
for (uint32_t i = 0; joined && i < stmt_count; i++)
|
||||
joined = py_import_join_statement(ctx, stmts[i], true);
|
||||
for (int i = 0; i < ctx->import_count; i++) {
|
||||
const char *local = ctx->import_local_names[i];
|
||||
const char *qn = ctx->import_module_qns[i];
|
||||
if (!local || !qn)
|
||||
continue;
|
||||
PyDirectImportKind direct_kind =
|
||||
py_import_kind_from_ast(ctx, have_root, stmts, stmt_count, local, &qn);
|
||||
py_import_kind_from_ast(ctx, have_root, joined, stmts, stmt_count, local, &qn);
|
||||
ctx->import_module_qns[i] = qn;
|
||||
if (ctx->import_kinds)
|
||||
ctx->import_kinds[i] = (unsigned char)direct_kind;
|
||||
@@ -1101,7 +1261,8 @@ static const char *py_exact_imported_reference_candidate(PyLSPContext *ctx,
|
||||
if (!ctx || !local_name)
|
||||
return NULL;
|
||||
const char *candidate = NULL;
|
||||
for (int i = 0; i < ctx->import_count; i++) {
|
||||
for (int i = py_import_row_first(ctx, local_name); i >= 0;
|
||||
i = py_import_row_next(ctx, local_name, i)) {
|
||||
const char *local = ctx->import_local_names[i];
|
||||
const char *qn = ctx->import_module_qns[i];
|
||||
if (!local || !qn || strcmp(local, local_name) != 0 ||
|
||||
@@ -3234,7 +3395,8 @@ static void py_emit_call_for(PyLSPContext *ctx, TSNode call_node) {
|
||||
* reason-based alias join by import metadata so an unrelated
|
||||
* local NAMED value cannot masquerade as an imported callable. */
|
||||
if (qn && qn_short && strcmp(qn_short, fname) != 0) {
|
||||
for (int i = 0; i < ctx->import_count; i++) {
|
||||
for (int i = py_import_row_first(ctx, fname); i >= 0;
|
||||
i = py_import_row_next(ctx, fname, i)) {
|
||||
const char *local = ctx->import_local_names[i];
|
||||
const char *import_qn = ctx->import_module_qns[i];
|
||||
if (local && import_qn && strcmp(local, fname) == 0 &&
|
||||
@@ -5687,7 +5849,7 @@ static void py_replay_import_local(PyLSPContext *ctx, TSNode stmt, const char *l
|
||||
PyDirectImportKind chosen_kind = PY_IMPORT_UNCLASSIFIED;
|
||||
const char *chosen_qn = NULL;
|
||||
bool conflicting_target = false;
|
||||
for (int i = 0; i < ctx->import_count; i++) {
|
||||
for (int i = py_import_row_first(ctx, local); i >= 0; i = py_import_row_next(ctx, local, i)) {
|
||||
if ((consumed && consumed[i]) || !ctx->import_local_names[i] ||
|
||||
!ctx->import_module_qns[i] || strcmp(ctx->import_local_names[i], local) != 0) {
|
||||
continue;
|
||||
@@ -5772,9 +5934,23 @@ static bool py_expression_is_annotation_only_assignment(TSNode statement) {
|
||||
return !ts_node_is_null(annotation) && ts_node_is_null(value);
|
||||
}
|
||||
|
||||
static void py_lsp_process_tree(PyLSPContext *ctx, TSNode root);
|
||||
|
||||
void py_lsp_process_file(PyLSPContext *ctx, TSNode root) {
|
||||
if (!ctx || ctx->eval_failure || ts_node_is_null(root))
|
||||
return;
|
||||
/* The import join holds this tree's nodes: it lives for this walk only. */
|
||||
CBMArena join_arena;
|
||||
cbm_arena_init_lazy(&join_arena, PY_IMPORT_JOIN_BLOCK);
|
||||
struct PyImportJoin join = {.arena = &join_arena};
|
||||
struct PyImportJoin *outer = ctx->import_join;
|
||||
ctx->import_join = &join;
|
||||
py_lsp_process_tree(ctx, root);
|
||||
ctx->import_join = outer;
|
||||
cbm_arena_destroy(&join_arena);
|
||||
}
|
||||
|
||||
static void py_lsp_process_tree(PyLSPContext *ctx, TSNode root) {
|
||||
int resolved_start = ctx->resolved_calls ? ctx->resolved_calls->count : 0;
|
||||
int synthetic_start = ctx->syn_calls ? ctx->syn_calls->count : 0;
|
||||
/* Only invocation dedup mutates prior rows, within this fixed tail.
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef CBM_LSP_PY_LSP_H
|
||||
#define CBM_LSP_PY_LSP_H
|
||||
|
||||
#include "lsp_name_rows.h"
|
||||
#include "type_rep.h"
|
||||
#include "scope.h"
|
||||
#include "type_registry.h"
|
||||
@@ -71,6 +72,10 @@ typedef struct {
|
||||
const char **import_module_qns;
|
||||
unsigned char *import_kinds; // internal PyDirectImportKind, classified once from the AST
|
||||
int import_count;
|
||||
int import_capacity; // rows the three arrays hold (py_lsp_add_import)
|
||||
CBMNameRows import_rows; // import_local_names by name, rows in table order
|
||||
// During one file's walk: the items of its import statements by the names they bind.
|
||||
struct PyImportJoin *import_join;
|
||||
|
||||
// Current function/class context for resolving `self`/`cls` and emitting
|
||||
// caller QNs.
|
||||
|
||||
+90
-12
@@ -26,6 +26,7 @@
|
||||
*/
|
||||
|
||||
#include "rust_lsp.h"
|
||||
#include "lsp_work.h"
|
||||
#include "rust_cargo.h"
|
||||
#include "../helpers.h"
|
||||
#include <ctype.h>
|
||||
@@ -328,12 +329,11 @@ static const char *rust_resolve_use(RustLSPContext *ctx, const char *local_name)
|
||||
if (!ctx || !local_name) {
|
||||
return NULL;
|
||||
}
|
||||
for (int i = 0; i < ctx->use_count; i++) {
|
||||
if (strcmp(ctx->use_local_names[i], local_name) == 0) {
|
||||
return ctx->use_module_paths[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
/* The first use of this local name (lsp_name_rows.h): a scan per use made a
|
||||
* file of thousands of `use` items quadratic. */
|
||||
int i = cbm_name_rows_first(&ctx->use_rows, ctx->arena, ctx->use_local_names, ctx->use_count,
|
||||
local_name);
|
||||
return i >= 0 ? ctx->use_module_paths[i] : NULL;
|
||||
}
|
||||
|
||||
/* The Rust prelude is auto-imported into every module. We map each name
|
||||
@@ -2648,6 +2648,7 @@ static const CBMRegisteredFunc *rust_lookup_method_in_trait(RustLSPContext *ctx,
|
||||
if (reg->fallback) {
|
||||
RustLSPContext fallback_ctx = *ctx;
|
||||
fallback_ctx.registry = reg->fallback;
|
||||
fallback_ctx.use_rows = (CBMNameRows){0}; /* the copy must not grow ctx's chains */
|
||||
return rust_lookup_method_in_trait(&fallback_ctx, trait_qn, method_name, allow_abstract);
|
||||
}
|
||||
return NULL;
|
||||
@@ -5554,6 +5555,51 @@ static void rust_collect_uses(RustLSPContext *ctx, TSNode root) {
|
||||
}
|
||||
}
|
||||
|
||||
/* The first function row of each key, for a pass that patches registered
|
||||
* functions while the function list does not change: the pass starts its old
|
||||
* loop at that row instead of the top. A row the memo cannot take (no memory)
|
||||
* sets `failed`, and every loop starts at the top again. Scratch memory. */
|
||||
enum { RUST_FIRST_ROWS_BLOCK = 4096 };
|
||||
|
||||
typedef struct {
|
||||
CBMIdxMemo first;
|
||||
CBMArena arena;
|
||||
bool failed;
|
||||
} RustFirstRows;
|
||||
|
||||
static void rust_first_rows_init(RustFirstRows *rows) {
|
||||
memset(rows, 0, sizeof(*rows));
|
||||
cbm_arena_init_lazy(&rows->arena, RUST_FIRST_ROWS_BLOCK);
|
||||
}
|
||||
|
||||
static void rust_first_rows_destroy(RustFirstRows *rows) {
|
||||
cbm_arena_destroy(&rows->arena);
|
||||
}
|
||||
|
||||
static void rust_first_rows_put(RustFirstRows *rows, const char *key, int row) {
|
||||
if (rows->failed)
|
||||
return;
|
||||
CBM_LSP_WORK(1);
|
||||
if (key)
|
||||
cbm_idxmemo_put_if_absent(&rows->first, &rows->arena, key, row);
|
||||
if (!key || cbm_idxmemo_get(&rows->first, key) < 0)
|
||||
rows->failed = true;
|
||||
}
|
||||
|
||||
/* Where a patch loop over `count` functions starts for this key. */
|
||||
static int rust_first_rows_start(const RustFirstRows *rows, const char *key, int count) {
|
||||
CBM_LSP_WORK(1);
|
||||
if (rows->failed || !key)
|
||||
return 0;
|
||||
int row = cbm_idxmemo_get(&rows->first, key);
|
||||
return row >= 0 ? row : count;
|
||||
}
|
||||
|
||||
/* A method's key: receiver and name, joined by a byte no name holds. */
|
||||
static const char *rust_method_key(RustFirstRows *rows, const char *receiver, const char *name) {
|
||||
return cbm_arena_sprintf(&rows->arena, "%s\x1f%s", receiver, name);
|
||||
}
|
||||
|
||||
/* Build the registry from the per-file `result->defs`, `result->impl_traits`,
|
||||
* and a Rust prelude seed. */
|
||||
void cbm_rust_build_local_registry(CBMArena *arena, CBMTypeRegistry *reg, CBMFileResult *result,
|
||||
@@ -5719,7 +5765,11 @@ void cbm_rust_build_local_registry(CBMArena *arena, CBMTypeRegistry *reg, CBMFil
|
||||
fld_count++;
|
||||
}
|
||||
if (fld_count > 0) {
|
||||
for (int ti = 0; ti < reg->type_count; ti++) {
|
||||
/* The type's first registration (type_idx); the loop starts there. */
|
||||
CBM_LSP_WORK(1);
|
||||
int first = cbm_idxmemo_get(&type_idx, type_qn);
|
||||
for (int ti = first >= 0 ? first : reg->type_count; ti < reg->type_count;
|
||||
ti++) {
|
||||
if (reg->types[ti].qualified_name &&
|
||||
strcmp(reg->types[ti].qualified_name, type_qn) == 0) {
|
||||
const char **names = (const char **)cbm_arena_alloc(
|
||||
@@ -5751,8 +5801,11 @@ void cbm_rust_build_local_registry(CBMArena *arena, CBMTypeRegistry *reg, CBMFil
|
||||
continue;
|
||||
const char *trait_qn = cbm_arena_sprintf(arena, "%s.%s", module_qn, tn);
|
||||
|
||||
/* Mark as interface and collect method names. */
|
||||
for (int ti = 0; ti < reg->type_count; ti++) {
|
||||
/* Mark as interface and collect method names: the trait's first
|
||||
* registration (type_idx); the loop starts there. */
|
||||
CBM_LSP_WORK(1);
|
||||
int first = cbm_idxmemo_get(&type_idx, trait_qn);
|
||||
for (int ti = first >= 0 ? first : reg->type_count; ti < reg->type_count; ti++) {
|
||||
if (!reg->types[ti].qualified_name)
|
||||
continue;
|
||||
if (strcmp(reg->types[ti].qualified_name, trait_qn) == 0) {
|
||||
@@ -5804,6 +5857,13 @@ void cbm_rust_build_local_registry(CBMArena *arena, CBMTypeRegistry *reg, CBMFil
|
||||
tmp.registry = reg;
|
||||
tmp.module_qn = module_qn;
|
||||
|
||||
/* The first free function of each QN; the patch loop starts there. */
|
||||
RustFirstRows free_rows;
|
||||
rust_first_rows_init(&free_rows);
|
||||
for (int k = 0; k < reg->func_count; k++) {
|
||||
if (reg->funcs[k].qualified_name && !reg->funcs[k].receiver_type)
|
||||
rust_first_rows_put(&free_rows, reg->funcs[k].qualified_name, k);
|
||||
}
|
||||
uint32_t rnc = ts_node_child_count(root);
|
||||
for (uint32_t i = 0; i < rnc; i++) {
|
||||
TSNode top = ts_node_child(root, i);
|
||||
@@ -5818,7 +5878,8 @@ void cbm_rust_build_local_registry(CBMArena *arena, CBMTypeRegistry *reg, CBMFil
|
||||
continue;
|
||||
const CBMType *ret = rust_parse_type_node(&tmp, rtn);
|
||||
const char *fn_qn = cbm_arena_sprintf(arena, "%s.%s", module_qn, fname);
|
||||
for (int k = 0; k < reg->func_count; k++) {
|
||||
for (int k = rust_first_rows_start(&free_rows, fn_qn, reg->func_count);
|
||||
k < reg->func_count; k++) {
|
||||
CBMRegisteredFunc *rf = ®->funcs[k];
|
||||
if (!rf->qualified_name)
|
||||
continue;
|
||||
@@ -5834,6 +5895,7 @@ void cbm_rust_build_local_registry(CBMArena *arena, CBMTypeRegistry *reg, CBMFil
|
||||
break;
|
||||
}
|
||||
}
|
||||
rust_first_rows_destroy(&free_rows);
|
||||
}
|
||||
|
||||
/* Phase A2: derive-macro synthesis.
|
||||
@@ -5963,7 +6025,10 @@ void cbm_rust_build_local_registry(CBMArena *arena, CBMTypeRegistry *reg, CBMFil
|
||||
* trait QN as an embedded_type on the receiver
|
||||
* AND synthesize the method entries. */
|
||||
CBMRegisteredType *rt = NULL;
|
||||
for (int ti = 0; ti < reg->type_count; ti++) {
|
||||
CBM_LSP_WORK(1);
|
||||
int first = cbm_idxmemo_get(&type_idx, d->qualified_name);
|
||||
for (int ti = first >= 0 ? first : reg->type_count; ti < reg->type_count;
|
||||
ti++) {
|
||||
if (reg->types[ti].qualified_name &&
|
||||
strcmp(reg->types[ti].qualified_name, d->qualified_name) == 0) {
|
||||
rt = ®->types[ti];
|
||||
@@ -6035,6 +6100,16 @@ void cbm_rust_build_local_registry(CBMArena *arena, CBMTypeRegistry *reg, CBMFil
|
||||
* registered functions have no return-type signature and chained
|
||||
* method calls (`File::open().read()`) break. */
|
||||
if (!ts_node_is_null(root)) {
|
||||
/* The first function of each receiver and name; the patch loop starts there. */
|
||||
RustFirstRows method_rows;
|
||||
rust_first_rows_init(&method_rows);
|
||||
for (int k = 0; k < reg->func_count; k++) {
|
||||
const CBMRegisteredFunc *rf = ®->funcs[k];
|
||||
if (rf->receiver_type && rf->short_name)
|
||||
rust_first_rows_put(
|
||||
&method_rows, rust_method_key(&method_rows, rf->receiver_type, rf->short_name),
|
||||
k);
|
||||
}
|
||||
uint32_t rnc = ts_node_child_count(root);
|
||||
for (uint32_t i = 0; i < rnc; i++) {
|
||||
TSNode top = ts_node_child(root, i);
|
||||
@@ -6080,7 +6155,9 @@ void cbm_rust_build_local_registry(CBMArena *arena, CBMTypeRegistry *reg, CBMFil
|
||||
ret = cbm_type_named(arena, type_qn);
|
||||
}
|
||||
/* Patch the registered function's signature. */
|
||||
for (int k = 0; k < reg->func_count; k++) {
|
||||
const char *key = type_qn ? rust_method_key(&method_rows, type_qn, mname) : NULL;
|
||||
for (int k = rust_first_rows_start(&method_rows, key, reg->func_count);
|
||||
k < reg->func_count; k++) {
|
||||
CBMRegisteredFunc *rf = ®->funcs[k];
|
||||
if (!rf->receiver_type || !rf->short_name)
|
||||
continue;
|
||||
@@ -6097,6 +6174,7 @@ void cbm_rust_build_local_registry(CBMArena *arena, CBMTypeRegistry *reg, CBMFil
|
||||
}
|
||||
}
|
||||
}
|
||||
rust_first_rows_destroy(&method_rows);
|
||||
}
|
||||
|
||||
/* Phase C: encode `impl Trait for Type` as `embedded_types` on the
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#ifndef CBM_LSP_RUST_LSP_H
|
||||
#define CBM_LSP_RUST_LSP_H
|
||||
|
||||
#include "lsp_name_rows.h"
|
||||
#include "type_rep.h"
|
||||
#include "scope.h"
|
||||
#include "type_registry.h"
|
||||
@@ -75,6 +76,7 @@ typedef struct {
|
||||
const char **use_local_names;
|
||||
const char **use_module_paths;
|
||||
int use_count;
|
||||
CBMNameRows use_rows; /* use_local_names by name, rows in table order */
|
||||
|
||||
const char **glob_module_qns;
|
||||
int glob_count;
|
||||
|
||||
+94
-62
@@ -27,6 +27,7 @@
|
||||
*/
|
||||
|
||||
#include "ts_lsp.h"
|
||||
#include "lsp_work.h"
|
||||
#include "../../../src/foundation/platform.h"
|
||||
#include "../helpers.h"
|
||||
#include "../../../src/foundation/log.h"
|
||||
@@ -1499,7 +1500,7 @@ static const CBMRegisteredFunc *ts_exact_imported_function(TSLSPContext *ctx, co
|
||||
return NULL;
|
||||
}
|
||||
const char *matched_module = NULL;
|
||||
for (int i = 0; i < ctx->import_count; i++) {
|
||||
for (int i = ts_import_first(ctx, name); i >= 0; i = ts_import_next(ctx, name, i)) {
|
||||
const char *local = ctx->import_local_names ? ctx->import_local_names[i] : NULL;
|
||||
const char *module_qn = ctx->import_module_qns ? ctx->import_module_qns[i] : NULL;
|
||||
if (!local || !module_qn || strcmp(local, name) != 0) {
|
||||
@@ -1554,7 +1555,7 @@ static const CBMType *type_of_identifier(TSLSPContext *ctx, const char *name) {
|
||||
// and look it up in the registry. If it's a registered func, return its signature;
|
||||
// if it's a registered type, return NAMED. Falls back to NAMED(module_qn) for
|
||||
// namespace-style imports where no specific symbol matches.
|
||||
for (int i = 0; i < ctx->import_count; i++) {
|
||||
for (int i = ts_import_first(ctx, name); i >= 0; i = ts_import_next(ctx, name, i)) {
|
||||
const char *local = ctx->import_local_names ? ctx->import_local_names[i] : NULL;
|
||||
const char *mqn = ctx->import_module_qns ? ctx->import_module_qns[i] : NULL;
|
||||
if (!local || !mqn || strcmp(local, name) != 0)
|
||||
@@ -2080,14 +2081,14 @@ static const CBMRegisteredFunc *ts_lookup_symbol_for_call(TSLSPContext *ctx, con
|
||||
|
||||
/* Map one local import binding to its module. Duplicate bindings from
|
||||
* different modules are ambiguous and deliberately fail closed. */
|
||||
static const char *ts_import_module_for_local(const TSLSPContext *ctx, const char *local_name,
|
||||
static const char *ts_import_module_for_local(TSLSPContext *ctx, const char *local_name,
|
||||
bool *out_matched) {
|
||||
if (out_matched)
|
||||
*out_matched = false;
|
||||
if (!ctx || !local_name)
|
||||
return NULL;
|
||||
const char *matched = NULL;
|
||||
for (int i = 0; i < ctx->import_count; i++) {
|
||||
for (int i = ts_import_first(ctx, local_name); i >= 0; i = ts_import_next(ctx, local_name, i)) {
|
||||
const char *local = ctx->import_local_names ? ctx->import_local_names[i] : NULL;
|
||||
const char *module_qn = ctx->import_module_qns ? ctx->import_module_qns[i] : NULL;
|
||||
if (!local || !module_qn || strcmp(local, local_name) != 0)
|
||||
@@ -2591,7 +2592,7 @@ static const CBMType *resolve_type_with_imports(TSLSPContext *ctx, const CBMType
|
||||
return cbm_type_named(ctx->arena, local_qn);
|
||||
}
|
||||
|
||||
for (int i = 0; i < ctx->import_count; i++) {
|
||||
for (int i = ts_import_first(ctx, bare); i >= 0; i = ts_import_next(ctx, bare, i)) {
|
||||
const char *lname = ctx->import_local_names ? ctx->import_local_names[i] : NULL;
|
||||
const char *mqn = ctx->import_module_qns ? ctx->import_module_qns[i] : NULL;
|
||||
if (!lname || !mqn)
|
||||
@@ -3061,7 +3062,7 @@ static void resolve_call_at(TSLSPContext *ctx, TSNode call_node) {
|
||||
}
|
||||
}
|
||||
// Imported default/named function.
|
||||
for (int i = 0; i < ctx->import_count; i++) {
|
||||
for (int i = ts_import_first(ctx, name); i >= 0; i = ts_import_next(ctx, name, i)) {
|
||||
const char *lname = ctx->import_local_names ? ctx->import_local_names[i] : NULL;
|
||||
const char *mqn = ctx->import_module_qns ? ctx->import_module_qns[i] : NULL;
|
||||
if (lname && mqn && strcmp(lname, name) == 0) {
|
||||
@@ -3399,7 +3400,7 @@ static void resolve_jsx_element(TSLSPContext *ctx, TSNode element_node) {
|
||||
}
|
||||
}
|
||||
// Try imports.
|
||||
for (int i = 0; i < ctx->import_count; i++) {
|
||||
for (int i = ts_import_first(ctx, tag_name); i >= 0; i = ts_import_next(ctx, tag_name, i)) {
|
||||
const char *lname = ctx->import_local_names ? ctx->import_local_names[i] : NULL;
|
||||
const char *mqn = ctx->import_module_qns ? ctx->import_module_qns[i] : NULL;
|
||||
if (lname && mqn && strcmp(lname, tag_name) == 0) {
|
||||
@@ -4036,25 +4037,33 @@ void ts_lsp_init(TSLSPContext *ctx, CBMArena *arena, const char *source, int sou
|
||||
ctx->debug = (debug_env && debug_env[0]);
|
||||
}
|
||||
|
||||
enum { TS_IMPORT_FIRST_CAPACITY = 8, TS_SCRATCH_ARENA_BLOCK = 4096 };
|
||||
|
||||
void ts_lsp_add_import(TSLSPContext *ctx, const char *local_name, const char *module_qn) {
|
||||
if (!ctx || !ctx->arena || !local_name || !module_qn)
|
||||
return;
|
||||
int new_count = ctx->import_count + 1;
|
||||
const char **names =
|
||||
(const char **)cbm_arena_alloc(ctx->arena, (size_t)new_count * sizeof(const char *));
|
||||
const char **qns =
|
||||
(const char **)cbm_arena_alloc(ctx->arena, (size_t)new_count * sizeof(const char *));
|
||||
if (!names || !qns)
|
||||
return;
|
||||
for (int i = 0; i < ctx->import_count; i++) {
|
||||
names[i] = ctx->import_local_names ? ctx->import_local_names[i] : NULL;
|
||||
qns[i] = ctx->import_module_qns ? ctx->import_module_qns[i] : NULL;
|
||||
/* The arrays double when full: copied on every import, a file of thousands of
|
||||
* imports was quadratic. */
|
||||
if (ctx->import_count == ctx->import_capacity) {
|
||||
int capacity = ctx->import_capacity ? ctx->import_capacity * 2 : TS_IMPORT_FIRST_CAPACITY;
|
||||
const char **names =
|
||||
(const char **)cbm_arena_alloc(ctx->arena, (size_t)capacity * sizeof(const char *));
|
||||
const char **qns =
|
||||
(const char **)cbm_arena_alloc(ctx->arena, (size_t)capacity * sizeof(const char *));
|
||||
if (!names || !qns)
|
||||
return;
|
||||
for (int i = 0; i < ctx->import_count; i++) {
|
||||
CBM_LSP_WORK(1);
|
||||
names[i] = ctx->import_local_names ? ctx->import_local_names[i] : NULL;
|
||||
qns[i] = ctx->import_module_qns ? ctx->import_module_qns[i] : NULL;
|
||||
}
|
||||
ctx->import_local_names = names;
|
||||
ctx->import_module_qns = qns;
|
||||
ctx->import_capacity = capacity;
|
||||
}
|
||||
names[ctx->import_count] = cbm_arena_strdup(ctx->arena, local_name);
|
||||
qns[ctx->import_count] = cbm_arena_strdup(ctx->arena, module_qn);
|
||||
ctx->import_local_names = names;
|
||||
ctx->import_module_qns = qns;
|
||||
ctx->import_count = new_count;
|
||||
ctx->import_local_names[ctx->import_count] = cbm_arena_strdup(ctx->arena, local_name);
|
||||
ctx->import_module_qns[ctx->import_count] = cbm_arena_strdup(ctx->arena, module_qn);
|
||||
ctx->import_count++;
|
||||
}
|
||||
|
||||
static void ts_lsp_add_import_alias(TSLSPContext *ctx, const char *local, const char *exported) {
|
||||
@@ -4742,6 +4751,23 @@ void cbm_ts_stdlib_register(CBMTypeRegistry *reg, CBMArena *arena) {
|
||||
|
||||
// ── Single-file entry point ───────────────────────────────────────────────────
|
||||
|
||||
/* The next function after `row` (-1: the first) whose receiver is `owner`, in
|
||||
* registration order: by name over `receivers` when it could be listed, else by
|
||||
* a scan. */
|
||||
static int ts_receiver_row_next(const CBMTypeRegistry *reg, CBMNameRows *rows, CBMArena *arena,
|
||||
const char **receivers, const char *owner, int row) {
|
||||
if (receivers) {
|
||||
return row < 0 ? cbm_name_rows_first(rows, arena, receivers, reg->func_count, owner)
|
||||
: cbm_name_rows_next(rows, receivers, reg->func_count, owner, row);
|
||||
}
|
||||
for (int fi = row + 1; fi < reg->func_count; fi++) {
|
||||
CBM_LSP_WORK(1);
|
||||
if (reg->funcs[fi].receiver_type && strcmp(reg->funcs[fi].receiver_type, owner) == 0)
|
||||
return fi;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Build a registry from result->defs: register Class/Interface as types; Function/Method
|
||||
// as funcs. Walks AST briefly for class fields/extends.
|
||||
static void register_file_defs(CBMArena *arena, CBMTypeRegistry *reg, CBMFileResult *result,
|
||||
@@ -4890,18 +4916,30 @@ static void register_file_defs(CBMArena *arena, CBMTypeRegistry *reg, CBMFileRes
|
||||
|
||||
// Second pass: attach method_names / method_qns to each registered type from
|
||||
// the registered methods. Lets dispatch find them via lookup_member_type.
|
||||
// A type's methods are found by receiver name (lsp_name_rows.h): a scan of every
|
||||
// function per type made a file of classes quadratic. The functions do not
|
||||
// change in this pass.
|
||||
CBMArena rows_arena;
|
||||
cbm_arena_init_lazy(&rows_arena, TS_SCRATCH_ARENA_BLOCK);
|
||||
CBMNameRows receiver_rows = {0};
|
||||
const char **receivers = reg->func_count > 0
|
||||
? (const char **)cbm_arena_alloc(
|
||||
&rows_arena, (size_t)reg->func_count * sizeof(*receivers))
|
||||
: NULL;
|
||||
for (int fi = 0; receivers && fi < reg->func_count; fi++)
|
||||
receivers[fi] = reg->funcs[fi].receiver_type;
|
||||
for (int ti = 0; ti < reg->type_count; ti++) {
|
||||
CBMRegisteredType *rt = ®->types[ti];
|
||||
if (!rt->qualified_name)
|
||||
continue;
|
||||
const char *owner = rt->qualified_name;
|
||||
|
||||
// Count methods first.
|
||||
int mcount = 0;
|
||||
for (int fi = 0; fi < reg->func_count; fi++) {
|
||||
const CBMRegisteredFunc *f = ®->funcs[fi];
|
||||
if (f->receiver_type && strcmp(f->receiver_type, rt->qualified_name) == 0) {
|
||||
mcount++;
|
||||
}
|
||||
for (int fi = ts_receiver_row_next(reg, &receiver_rows, &rows_arena, receivers, owner, -1);
|
||||
fi >= 0;
|
||||
fi = ts_receiver_row_next(reg, &receiver_rows, &rows_arena, receivers, owner, fi)) {
|
||||
mcount++;
|
||||
}
|
||||
if (mcount == 0)
|
||||
continue;
|
||||
@@ -4913,19 +4951,20 @@ static void register_file_defs(CBMArena *arena, CBMTypeRegistry *reg, CBMFileRes
|
||||
if (!mnames || !mqns)
|
||||
continue;
|
||||
int idx = 0;
|
||||
for (int fi = 0; fi < reg->func_count && idx < mcount; fi++) {
|
||||
for (int fi = ts_receiver_row_next(reg, &receiver_rows, &rows_arena, receivers, owner, -1);
|
||||
fi >= 0 && idx < mcount;
|
||||
fi = ts_receiver_row_next(reg, &receiver_rows, &rows_arena, receivers, owner, fi)) {
|
||||
const CBMRegisteredFunc *f = ®->funcs[fi];
|
||||
if (f->receiver_type && strcmp(f->receiver_type, rt->qualified_name) == 0) {
|
||||
mnames[idx] = f->short_name;
|
||||
mqns[idx] = f->qualified_name;
|
||||
idx++;
|
||||
}
|
||||
mnames[idx] = f->short_name;
|
||||
mqns[idx] = f->qualified_name;
|
||||
idx++;
|
||||
}
|
||||
mnames[mcount] = NULL;
|
||||
mqns[mcount] = NULL;
|
||||
rt->method_names = mnames;
|
||||
rt->method_qns = mqns;
|
||||
}
|
||||
cbm_arena_destroy(&rows_arena);
|
||||
}
|
||||
|
||||
// AST walk stacks for the passes below. The first chunk lives on the C stack, the
|
||||
@@ -5566,7 +5605,9 @@ static void apply_jsdoc_signatures(TSLSPContext *ctx, TSNode root, CBMTypeRegist
|
||||
continue;
|
||||
const char *fqn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->module_qn, fname);
|
||||
|
||||
for (int fi = 0; fi < reg->func_count; fi++) {
|
||||
/* The loop patches the first registration of fqn; start there. */
|
||||
const CBMRegisteredFunc *own = cbm_registry_first_own_func(reg, fqn);
|
||||
for (int fi = own ? (int)(own - reg->funcs) : reg->func_count; fi < reg->func_count; fi++) {
|
||||
if (!reg->funcs[fi].qualified_name)
|
||||
continue;
|
||||
if (strcmp(reg->funcs[fi].qualified_name, fqn) != 0)
|
||||
@@ -5667,15 +5708,10 @@ static void infer_implicit_returns(TSLSPContext *ctx, TSNode root, CBMTypeRegist
|
||||
const CBMType *rets[2] = {inferred, NULL};
|
||||
const CBMType *new_sig = cbm_type_func(ctx->arena, param_names, param_types, rets);
|
||||
|
||||
// Patch the registered func in place.
|
||||
for (int fi = 0; fi < reg->func_count; fi++) {
|
||||
if (reg->funcs[fi].qualified_name == fqn ||
|
||||
(reg->funcs[fi].qualified_name &&
|
||||
strcmp(reg->funcs[fi].qualified_name, fqn) == 0)) {
|
||||
reg->funcs[fi].signature = new_sig;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Patch the registered func in place: its first registration.
|
||||
CBMRegisteredFunc *patched = cbm_registry_first_own_func(reg, fqn);
|
||||
if (patched)
|
||||
patched->signature = new_sig;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5873,17 +5909,10 @@ static void ast_sweep_shapes(TSLSPContext *ctx, TSNode root, CBMTypeRegistry *re
|
||||
}
|
||||
}
|
||||
// Find or insert.
|
||||
bool found = false;
|
||||
for (int ti = 0; ti < reg->type_count; ti++) {
|
||||
if (reg->types[ti].qualified_name &&
|
||||
strcmp(reg->types[ti].qualified_name, qn) == 0) {
|
||||
if (alias_qn)
|
||||
reg->types[ti].alias_of = alias_qn;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
CBMRegisteredType *known = cbm_registry_first_own_type(reg, qn);
|
||||
if (known && alias_qn)
|
||||
known->alias_of = alias_qn;
|
||||
if (!known) {
|
||||
CBMRegisteredType rt;
|
||||
memset(&rt, 0, sizeof(rt));
|
||||
rt.qualified_name = qn;
|
||||
@@ -5908,14 +5937,7 @@ static void ast_sweep_shapes(TSLSPContext *ctx, TSNode root, CBMTypeRegistry *re
|
||||
const char *class_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->module_qn, cname);
|
||||
|
||||
// Find the registered type and augment with fields.
|
||||
CBMRegisteredType *rt = NULL;
|
||||
for (int ti = 0; ti < reg->type_count; ti++) {
|
||||
if (reg->types[ti].qualified_name &&
|
||||
strcmp(reg->types[ti].qualified_name, class_qn) == 0) {
|
||||
rt = ®->types[ti];
|
||||
break;
|
||||
}
|
||||
}
|
||||
CBMRegisteredType *rt = cbm_registry_first_own_type(reg, class_qn);
|
||||
if (!rt)
|
||||
continue;
|
||||
|
||||
@@ -6046,7 +6068,16 @@ void cbm_run_ts_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
|
||||
CBMTypeRegistry reg;
|
||||
cbm_registry_init(®, arena);
|
||||
cbm_ts_stdlib_register(®, arena);
|
||||
/* The passes below ask about every definition of the file, while it fills and
|
||||
* after. It is indexed once its defs are in, with the first registration
|
||||
* answering as a scan does, and what the passes add after that is found by
|
||||
* name behind the index. The index memory is scratch: nothing reads it later. */
|
||||
reg.index_first_registered = true;
|
||||
CBMArena idx_arena;
|
||||
cbm_arena_init_lazy(&idx_arena, TS_SCRATCH_ARENA_BLOCK);
|
||||
reg.tail_arena = &idx_arena;
|
||||
register_file_defs(arena, ®, result, result->module_qn);
|
||||
cbm_registry_finalize_into(®, &idx_arena);
|
||||
|
||||
TSLSPContext ctx;
|
||||
ts_lsp_init(&ctx, arena, source, source_len, ®, result->module_qn, js_mode, jsx_mode,
|
||||
@@ -6070,6 +6101,7 @@ void cbm_run_ts_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
|
||||
infer_implicit_returns(&ctx, root, ®);
|
||||
|
||||
ts_lsp_process_file(&ctx, root);
|
||||
cbm_arena_destroy(&idx_arena);
|
||||
}
|
||||
|
||||
// ── Cross-file entry point (Phase 3) ──────────────────────────────────────────
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef CBM_LSP_TS_LSP_H
|
||||
#define CBM_LSP_TS_LSP_H
|
||||
|
||||
#include "lsp_name_rows.h"
|
||||
#include "type_rep.h"
|
||||
#include "scope.h"
|
||||
#include "type_registry.h"
|
||||
@@ -32,6 +33,8 @@ typedef struct {
|
||||
const char **import_local_names;
|
||||
const char **import_module_qns;
|
||||
int import_count;
|
||||
int import_capacity; // rows the two arrays hold (ts_lsp_add_import)
|
||||
CBMNameRows import_rows; // import_local_names by name, rows in table order
|
||||
|
||||
// Renamed named imports (`import { foo as bar }`): local binding -> the name
|
||||
// the module exports. Parallel arrays of length import_alias_count; a
|
||||
@@ -75,6 +78,16 @@ typedef struct {
|
||||
struct TsEvalMemo *eval_memo;
|
||||
} TSLSPContext;
|
||||
|
||||
/* The imports with this local name, in table order (lsp_name_rows.h). */
|
||||
static inline int ts_import_first(TSLSPContext *ctx, const char *name) {
|
||||
return cbm_name_rows_first(&ctx->import_rows, ctx->arena, ctx->import_local_names,
|
||||
ctx->import_count, name);
|
||||
}
|
||||
static inline int ts_import_next(const TSLSPContext *ctx, const char *name, int row) {
|
||||
return cbm_name_rows_next(&ctx->import_rows, ctx->import_local_names, ctx->import_count, name,
|
||||
row);
|
||||
}
|
||||
|
||||
#ifdef CBM_ENABLE_TEST_SEAMS
|
||||
// Complexity-regression seam: remaining expression-eval budget / warned flag
|
||||
// for the calling thread, valid after a cbm_run_ts_lsp on the same thread.
|
||||
|
||||
@@ -351,6 +351,91 @@ static const CBMRegisteredType *lookup_type_self(const CBMTypeRegistry *reg,
|
||||
static const CBMRegisteredFunc *lookup_func_self(const CBMTypeRegistry *reg,
|
||||
const char *qualified_name);
|
||||
|
||||
/* A tail this short is scanned; it is not worth a memo. */
|
||||
enum { CBM_REGISTRY_TAIL_SCAN = 64 };
|
||||
|
||||
/* The first row from `from` on that registered the name, or -1. The memo answers
|
||||
* for the rows it holds, and the rows behind it are scanned. */
|
||||
static int tail_first_func(const CBMTypeRegistry *reg, int from, const char *qualified_name) {
|
||||
const CBMRegistryTail *tail = ®->func_tail;
|
||||
if (tail->start == from && tail->synced > from) {
|
||||
CBM_LSP_WORK(1);
|
||||
int row = cbm_idxmemo_get(&tail->first, qualified_name);
|
||||
if (row >= 0)
|
||||
return row;
|
||||
from = tail->synced;
|
||||
}
|
||||
for (int i = from; i < reg->func_count; i++) {
|
||||
CBM_LSP_WORK(1);
|
||||
if (reg->funcs[i].qualified_name &&
|
||||
strcmp(reg->funcs[i].qualified_name, qualified_name) == 0)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int tail_first_type(const CBMTypeRegistry *reg, int from, const char *qualified_name) {
|
||||
const CBMRegistryTail *tail = ®->type_tail;
|
||||
if (tail->start == from && tail->synced > from) {
|
||||
CBM_LSP_WORK(1);
|
||||
int row = cbm_idxmemo_get(&tail->first, qualified_name);
|
||||
if (row >= 0)
|
||||
return row;
|
||||
from = tail->synced;
|
||||
}
|
||||
for (int i = from; i < reg->type_count; i++) {
|
||||
CBM_LSP_WORK(1);
|
||||
if (reg->types[i].qualified_name &&
|
||||
strcmp(reg->types[i].qualified_name, qualified_name) == 0)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Brings a long tail's memo up to date. An index built since starts it over, and a
|
||||
* row the memo could not take stays behind `synced`, where lookups scan for it. */
|
||||
static void tail_sync_funcs(CBMTypeRegistry *reg) {
|
||||
CBMRegistryTail *tail = ®->func_tail;
|
||||
int from = reg->func_qn_buckets && reg->func_qn_bucket_count > 0 ? reg->func_qn_entry_count : 0;
|
||||
if (!reg->tail_arena || reg->func_count - from < CBM_REGISTRY_TAIL_SCAN)
|
||||
return;
|
||||
if (tail->start != from) {
|
||||
memset(&tail->first, 0, sizeof(tail->first));
|
||||
tail->start = from;
|
||||
tail->synced = from;
|
||||
}
|
||||
for (; tail->synced < reg->func_count; tail->synced++) {
|
||||
const char *qn = reg->funcs[tail->synced].qualified_name;
|
||||
if (!qn)
|
||||
continue;
|
||||
CBM_LSP_WORK(1);
|
||||
cbm_idxmemo_put_if_absent(&tail->first, reg->tail_arena, qn, tail->synced);
|
||||
if (cbm_idxmemo_get(&tail->first, qn) < 0)
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void tail_sync_types(CBMTypeRegistry *reg) {
|
||||
CBMRegistryTail *tail = ®->type_tail;
|
||||
int from = reg->type_qn_buckets && reg->type_qn_bucket_count > 0 ? reg->type_qn_entry_count : 0;
|
||||
if (!reg->tail_arena || reg->type_count - from < CBM_REGISTRY_TAIL_SCAN)
|
||||
return;
|
||||
if (tail->start != from) {
|
||||
memset(&tail->first, 0, sizeof(tail->first));
|
||||
tail->start = from;
|
||||
tail->synced = from;
|
||||
}
|
||||
for (; tail->synced < reg->type_count; tail->synced++) {
|
||||
const char *qn = reg->types[tail->synced].qualified_name;
|
||||
if (!qn)
|
||||
continue;
|
||||
CBM_LSP_WORK(1);
|
||||
cbm_idxmemo_put_if_absent(&tail->first, reg->tail_arena, qn, tail->synced);
|
||||
if (cbm_idxmemo_get(&tail->first, qn) < 0)
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* A base entry is shadowed when the head holds an entry with the same QN. */
|
||||
static bool type_shadowed(const CBMTypeRegistry *shadow, const CBMTypeRegistry *reg, int i) {
|
||||
if (!shadow || shadow == reg || !reg->types[i].qualified_name) {
|
||||
@@ -727,6 +812,66 @@ void cbm_registry_finalize_into(CBMTypeRegistry *reg, CBMArena *idx_arena) {
|
||||
build_type_short_index(reg, idx_arena);
|
||||
}
|
||||
|
||||
void cbm_registry_index_types_into(CBMTypeRegistry *reg, CBMArena *idx_arena) {
|
||||
if (!reg || !idx_arena)
|
||||
return;
|
||||
bool first = reg->index_first_registered;
|
||||
reg->index_first_registered = true;
|
||||
build_qn_index(reg, idx_arena, /*for_funcs=*/false);
|
||||
build_type_short_index(reg, idx_arena);
|
||||
reg->index_first_registered = first;
|
||||
}
|
||||
|
||||
CBMRegisteredFunc *cbm_registry_first_own_func(CBMTypeRegistry *reg, const char *qualified_name) {
|
||||
if (!reg || !qualified_name) {
|
||||
return NULL;
|
||||
}
|
||||
int indexed = 0, lowest = -1;
|
||||
if (reg->func_qn_buckets && reg->func_qn_bucket_count > 0) {
|
||||
indexed = reg->func_qn_entry_count;
|
||||
uint64_t h = fnv1a(qualified_name);
|
||||
int slot = (int)(h & (uint64_t)(reg->func_qn_bucket_count - 1));
|
||||
for (int idx = reg->func_qn_buckets[slot]; idx >= 0;
|
||||
idx = reg->func_qn_entries[idx].next_index) {
|
||||
CBM_LSP_WORK(1);
|
||||
int p = reg->func_qn_entries[idx].payload_index;
|
||||
if (reg->func_qn_entries[idx].hash == h && reg->funcs[p].qualified_name &&
|
||||
strcmp(reg->funcs[p].qualified_name, qualified_name) == 0 &&
|
||||
(lowest < 0 || p < lowest)) {
|
||||
lowest = p;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (lowest < 0)
|
||||
lowest = tail_first_func(reg, indexed, qualified_name);
|
||||
return lowest >= 0 ? ®->funcs[lowest] : NULL;
|
||||
}
|
||||
|
||||
CBMRegisteredType *cbm_registry_first_own_type(CBMTypeRegistry *reg, const char *qualified_name) {
|
||||
if (!reg || !qualified_name) {
|
||||
return NULL;
|
||||
}
|
||||
int indexed = 0, lowest = -1;
|
||||
if (reg->type_qn_buckets && reg->type_qn_bucket_count > 0) {
|
||||
indexed = reg->type_qn_entry_count;
|
||||
uint64_t h = fnv1a(qualified_name);
|
||||
int slot = (int)(h & (uint64_t)(reg->type_qn_bucket_count - 1));
|
||||
for (int idx = reg->type_qn_buckets[slot]; idx >= 0;
|
||||
idx = reg->type_qn_entries[idx].next_index) {
|
||||
CBM_LSP_WORK(1);
|
||||
int p = reg->type_qn_entries[idx].payload_index;
|
||||
if (reg->type_qn_entries[idx].hash == h && reg->types[p].qualified_name &&
|
||||
strcmp(reg->types[p].qualified_name, qualified_name) == 0 &&
|
||||
(lowest < 0 || p < lowest)) {
|
||||
lowest = p;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (lowest < 0)
|
||||
lowest = tail_first_type(reg, indexed, qualified_name);
|
||||
return lowest >= 0 ? ®->types[lowest] : NULL;
|
||||
}
|
||||
|
||||
void cbm_registry_finalize(CBMTypeRegistry *reg) {
|
||||
if (!reg)
|
||||
return;
|
||||
@@ -756,6 +901,7 @@ void cbm_registry_add_func(CBMTypeRegistry *reg, CBMRegisteredFunc func) {
|
||||
reg->func_cap = new_cap;
|
||||
}
|
||||
reg->funcs[reg->func_count++] = func;
|
||||
tail_sync_funcs(reg);
|
||||
}
|
||||
|
||||
void cbm_registry_add_type(CBMTypeRegistry *reg, CBMRegisteredType type) {
|
||||
@@ -776,6 +922,7 @@ void cbm_registry_add_type(CBMTypeRegistry *reg, CBMRegisteredType type) {
|
||||
reg->type_cap = new_cap;
|
||||
}
|
||||
reg->types[reg->type_count++] = type;
|
||||
tail_sync_types(reg);
|
||||
}
|
||||
|
||||
static const CBMRegisteredFunc *lookup_method_self(const CBMTypeRegistry *reg,
|
||||
@@ -850,23 +997,12 @@ static const CBMRegisteredType *lookup_type_self(const CBMTypeRegistry *reg,
|
||||
}
|
||||
}
|
||||
/* Tail-scan types added after finalize (see lookup_method_self). */
|
||||
for (int i = reg->type_qn_entry_count; i < reg->type_count; i++) {
|
||||
CBM_LSP_WORK(1);
|
||||
if (reg->types[i].qualified_name &&
|
||||
strcmp(reg->types[i].qualified_name, qualified_name) == 0) {
|
||||
return ®->types[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
int tail_row = tail_first_type(reg, reg->type_qn_entry_count, qualified_name);
|
||||
return tail_row >= 0 ? ®->types[tail_row] : NULL;
|
||||
}
|
||||
|
||||
for (int i = 0; i < reg->type_count; i++) {
|
||||
CBM_LSP_WORK(1);
|
||||
if (strcmp(reg->types[i].qualified_name, qualified_name) == 0) {
|
||||
return ®->types[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
int row = tail_first_type(reg, 0, qualified_name);
|
||||
return row >= 0 ? ®->types[row] : NULL;
|
||||
}
|
||||
|
||||
const CBMRegisteredType *cbm_registry_lookup_type(const CBMTypeRegistry *reg,
|
||||
@@ -897,24 +1033,12 @@ static const CBMRegisteredFunc *lookup_func_self(const CBMTypeRegistry *reg,
|
||||
}
|
||||
}
|
||||
/* Tail-scan funcs added after finalize (see lookup_method_self). */
|
||||
for (int i = reg->func_qn_entry_count; i < reg->func_count; i++) {
|
||||
CBM_LSP_WORK(1);
|
||||
if (reg->funcs[i].qualified_name &&
|
||||
strcmp(reg->funcs[i].qualified_name, qualified_name) == 0) {
|
||||
return ®->funcs[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
int tail_row = tail_first_func(reg, reg->func_qn_entry_count, qualified_name);
|
||||
return tail_row >= 0 ? ®->funcs[tail_row] : NULL;
|
||||
}
|
||||
|
||||
for (int i = 0; i < reg->func_count; i++) {
|
||||
CBM_LSP_WORK(1);
|
||||
if (reg->funcs[i].qualified_name &&
|
||||
strcmp(reg->funcs[i].qualified_name, qualified_name) == 0) {
|
||||
return ®->funcs[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
int row = tail_first_func(reg, 0, qualified_name);
|
||||
return row >= 0 ? ®->funcs[row] : NULL;
|
||||
}
|
||||
|
||||
const CBMRegisteredFunc *cbm_registry_lookup_func(const CBMTypeRegistry *reg,
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include "type_rep.h"
|
||||
#include "../arena.h"
|
||||
#include "lsp_neg_memo.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
// Language-specific function metadata. Added at struct tail so existing
|
||||
@@ -95,6 +96,13 @@ typedef struct {
|
||||
int type_index;
|
||||
} CBMRegistryTypeQNEntry;
|
||||
|
||||
// The rows of funcs[] or types[] past their QN index, by name (see tail_arena).
|
||||
typedef struct {
|
||||
CBMIdxMemo first; // a QN's first row among [start, synced)
|
||||
int start;
|
||||
int synced;
|
||||
} CBMRegistryTail;
|
||||
|
||||
// Cross-file type/function registry.
|
||||
typedef struct CBMTypeRegistry {
|
||||
CBMRegisteredFunc *funcs;
|
||||
@@ -126,6 +134,13 @@ typedef struct CBMTypeRegistry {
|
||||
int type_qn_bucket_count;
|
||||
int type_qn_entry_count;
|
||||
|
||||
// Set by a caller whose lookups run while the registry fills: the rows past
|
||||
// the QN indexes are then found by name instead of by a scan, with the same
|
||||
// answers. The arena must outlive those lookups. NULL keeps the scan.
|
||||
CBMArena *tail_arena;
|
||||
CBMRegistryTail func_tail;
|
||||
CBMRegistryTail type_tail;
|
||||
|
||||
// Methods indexed by (receiver_type, short_name) — chain holds overloads.
|
||||
int *method_buckets;
|
||||
CBMRegistryHashEntry *method_entries;
|
||||
@@ -212,6 +227,9 @@ void cbm_registry_finalize(CBMTypeRegistry *reg);
|
||||
// pipeline-lifetime result arena, and per-file index allocations accumulated
|
||||
// there add GBs across a large repo (FastAPI incremental test: +1.1 GB RSS).
|
||||
void cbm_registry_finalize_into(CBMTypeRegistry *reg, CBMArena *idx_arena);
|
||||
// Indexes only the types, for a registry that is still filling. A scan finds the first
|
||||
// registration, so these answer with that one; a later finalize replaces them.
|
||||
void cbm_registry_index_types_into(CBMTypeRegistry *reg, CBMArena *idx_arena);
|
||||
|
||||
// Build the optional final-QN-segment type index. Call immediately after
|
||||
// finalization. Consumers that do not opt in avoid its type_count-sized allocation
|
||||
@@ -463,6 +481,10 @@ void cbm_registry_all_types_chain(const CBMTypeRegistry *head, CBMTypeShortIter
|
||||
* sealed-registry no-op in cbm_registry_add_func does today. */
|
||||
CBMRegisteredFunc *cbm_registry_func_for_update(CBMTypeRegistry *head, const char *qualified_name);
|
||||
CBMRegisteredType *cbm_registry_type_for_update(CBMTypeRegistry *head, const char *qualified_name);
|
||||
/* The first function or type registered under this name in reg itself, whichever order
|
||||
* its index answers lookups in; a fallback registry is not consulted. */
|
||||
CBMRegisteredFunc *cbm_registry_first_own_func(CBMTypeRegistry *reg, const char *qualified_name);
|
||||
CBMRegisteredType *cbm_registry_first_own_type(CBMTypeRegistry *reg, const char *qualified_name);
|
||||
|
||||
// --- TS-specific helpers (return NULL for types without these signatures) ---
|
||||
|
||||
|
||||
@@ -50,6 +50,8 @@
|
||||
#include "cbm.h"
|
||||
#include "discover/discover.h"
|
||||
#include "lang_specs.h"
|
||||
#include "lsp/lsp_name_rows.h"
|
||||
#include "lsp/lsp_work.h"
|
||||
#include "lsp/py_lsp.h"
|
||||
#include "tree_sitter/api.h"
|
||||
#include "pipeline/pass_lsp_cross.h"
|
||||
@@ -1903,6 +1905,551 @@ TEST(complexity_go_route_bindings_are_near_linear) {
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* ── Language passes: imports and registrations found by name ───────────
|
||||
* Extraction runs the file's own language pass in-process. Its searches of the
|
||||
* import table and the registry charge lsp_work.h, one step per row compared or
|
||||
* copied, so a pass that searches per definition or per use shows about 4x when
|
||||
* the rows double, and one that asks by name about 2x. */
|
||||
static uint64_t cx_fnv(uint64_t hash, const char *text) {
|
||||
for (const char *at = text ? text : "\x01"; *at; at++)
|
||||
hash = (hash ^ (unsigned char)*at) * UINT64_C(0x100000001B3);
|
||||
return (hash ^ 0xff) * UINT64_C(0x100000001B3);
|
||||
}
|
||||
|
||||
/* What the pass resolved, in order: a change of answer changes this digest. */
|
||||
static uint64_t cx_resolved_digest(const CBMResolvedCallArray *calls) {
|
||||
uint64_t hash = UINT64_C(0xcbf29ce484222325);
|
||||
for (int i = 0; i < calls->count; i++) {
|
||||
const CBMResolvedCall *call = &calls->items[i];
|
||||
char numbers[64];
|
||||
snprintf(numbers, sizeof(numbers), "%d:%u:%u:%.3f", (int)call->kind, call->site_start_byte,
|
||||
call->site_end_byte, (double)call->confidence);
|
||||
hash = cx_fnv(hash, call->caller_qn);
|
||||
hash = cx_fnv(hash, call->callee_qn);
|
||||
hash = cx_fnv(hash, call->strategy);
|
||||
hash = cx_fnv(hash, call->reason);
|
||||
hash = cx_fnv(hash, numbers);
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
static uint64_t cx_lsp_steps_of(CBMLanguage language, const char *path, const char *source,
|
||||
int *resolved, uint64_t *digest) {
|
||||
(void)cbm_lsp_work_take();
|
||||
CBMFileResult *result =
|
||||
cbm_extract_file(source, (int)strlen(source), language, "test", path, 0, NULL, NULL);
|
||||
uint64_t work = cbm_lsp_work_take();
|
||||
*resolved = result ? result->resolved_calls.count : -1;
|
||||
*digest = result ? cx_resolved_digest(&result->resolved_calls) : 0;
|
||||
if (!result || result->lsp_skipped)
|
||||
work = 0;
|
||||
if (result)
|
||||
cbm_free_result(result);
|
||||
return work;
|
||||
}
|
||||
|
||||
static uint64_t cx_extract_lsp_steps(CBMLanguage language, const char *path, const char *head,
|
||||
CxLineFn line, const char *tail, int lines, int *resolved,
|
||||
uint64_t *digest) {
|
||||
char *source = cx_lines_text(head, line, tail, lines);
|
||||
if (!source)
|
||||
return 0;
|
||||
uint64_t work = cx_lsp_steps_of(language, path, source, resolved, digest);
|
||||
free(source);
|
||||
return work;
|
||||
}
|
||||
|
||||
/* Steps at twice the rows over steps at CX_BINDING_LINES rows; *base gets the latter.
|
||||
* The resolved rows and their digest are printed so two builds can be compared. */
|
||||
static double cx_pass_ratio(CBMLanguage language, const char *path, const char *head, CxLineFn line,
|
||||
const char *tail, uint64_t *base) {
|
||||
int resolved[2];
|
||||
uint64_t digest[2];
|
||||
uint64_t first = cx_extract_lsp_steps(language, path, head, line, tail, CX_BINDING_LINES,
|
||||
&resolved[0], &digest[0]);
|
||||
uint64_t second = cx_extract_lsp_steps(language, path, head, line, tail, CX_BINDING_LINES * 2,
|
||||
&resolved[1], &digest[1]);
|
||||
double ratio = cx_ratio((double)second, (double)first);
|
||||
printf(" %s lsp steps %llu -> %llu (ratio %.2f); resolved %d/%016llx, %d/%016llx\n", path,
|
||||
(unsigned long long)first, (unsigned long long)second, ratio, resolved[0],
|
||||
(unsigned long long)digest[0], resolved[1], (unsigned long long)digest[1]);
|
||||
*base = first;
|
||||
return ratio;
|
||||
}
|
||||
|
||||
/* The table says the same whichever way a name is found. */
|
||||
TEST(complexity_name_rows_answer_as_a_scan_does) {
|
||||
enum { COUNT = 64 };
|
||||
const char *names[COUNT];
|
||||
char storage[COUNT][8];
|
||||
for (int i = 0; i < COUNT; i++) {
|
||||
snprintf(storage[i], sizeof(storage[i]), "n%d", i % 7);
|
||||
names[i] = i % 11 == 5 ? NULL : storage[i];
|
||||
}
|
||||
CBMArena arena;
|
||||
cbm_arena_init(&arena);
|
||||
CBMNameRows rows = {0};
|
||||
bool same = true;
|
||||
/* Both short of the threshold and past it; the table may grow between questions. */
|
||||
for (int count = 1; count <= COUNT; count++) {
|
||||
for (int v = 0; v < 8; v++) {
|
||||
char name[8];
|
||||
snprintf(name, sizeof(name), "n%d", v);
|
||||
int expected = -1;
|
||||
for (int i = 0; i < count && expected < 0; i++)
|
||||
if (names[i] && !strcmp(names[i], name))
|
||||
expected = i;
|
||||
int got = cbm_name_rows_first(&rows, &arena, names, count, name);
|
||||
while (same && expected >= 0) {
|
||||
same = got == expected;
|
||||
int after = -1;
|
||||
for (int i = expected + 1; i < count && after < 0; i++)
|
||||
if (names[i] && !strcmp(names[i], name))
|
||||
after = i;
|
||||
got = cbm_name_rows_next(&rows, names, count, name, got);
|
||||
expected = after;
|
||||
}
|
||||
same = same && got == -1;
|
||||
}
|
||||
}
|
||||
cbm_arena_destroy(&arena);
|
||||
ASSERT_TRUE(same);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* Python's own pass copied its import table on every import, joined each import
|
||||
* to every import statement of the module and each statement's items to every
|
||||
* import, and scanned the table for each reference. */
|
||||
static const char *cx_line_py_import_pair(char *out, size_t cap, int i) {
|
||||
snprintf(out, cap, "import pkg_%d.part\nfrom mod_%d import name_%d\nname_%d()\n", i, i, i, i);
|
||||
return out;
|
||||
}
|
||||
static const char *cx_line_py_from_item(char *out, size_t cap, int i) {
|
||||
snprintf(out, cap, " name_%d,\n", i);
|
||||
return out;
|
||||
}
|
||||
TEST(complexity_python_pass_import_join_is_linear) {
|
||||
uint64_t base = 0;
|
||||
double ratio =
|
||||
cx_pass_ratio(CBM_LANG_PYTHON, "pairs.py", "", cx_line_py_import_pair, "", &base);
|
||||
ASSERT_GTE(base, (uint64_t)CX_BINDING_LINES);
|
||||
ASSERT_TRUE(ratio >= CX_RATIO_LO && ratio <= CX_RATIO_HI);
|
||||
PASS();
|
||||
}
|
||||
/* One statement can hold them all: generated SDK packages re-export thousands of names. */
|
||||
TEST(complexity_python_pass_wide_from_import_is_linear) {
|
||||
uint64_t base = 0;
|
||||
double ratio = cx_pass_ratio(CBM_LANG_PYTHON, "reexport.py", "from .models import (\n",
|
||||
cx_line_py_from_item, ")\n", &base);
|
||||
ASSERT_GTE(base, (uint64_t)CX_BINDING_LINES);
|
||||
ASSERT_TRUE(ratio >= CX_RATIO_LO && ratio <= CX_RATIO_HI);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* Imports, then a use of each: every use scanned the imports. */
|
||||
typedef struct {
|
||||
CBMLanguage language;
|
||||
const char *path, *head, *imported, *middle, *used, *tail;
|
||||
} CxImportShape;
|
||||
|
||||
static uint64_t cx_import_shape_steps(const CxImportShape *shape, int lines, int *resolved,
|
||||
uint64_t *digest) {
|
||||
size_t room = strlen(shape->head) + strlen(shape->middle) + strlen(shape->tail) +
|
||||
(size_t)lines * CX_BINDING_LINE_ROOM * 2 + 1;
|
||||
char *source = malloc(room);
|
||||
if (!source)
|
||||
return 0;
|
||||
size_t length = (size_t)snprintf(source, room, "%s", shape->head);
|
||||
for (int i = 0; i < lines; i++)
|
||||
length += (size_t)snprintf(source + length, room - length, shape->imported, i, i);
|
||||
length += (size_t)snprintf(source + length, room - length, "%s", shape->middle);
|
||||
for (int i = 0; i < lines; i++)
|
||||
length += (size_t)snprintf(source + length, room - length, shape->used, i, i);
|
||||
(void)snprintf(source + length, room - length, "%s", shape->tail);
|
||||
uint64_t work = cx_lsp_steps_of(shape->language, shape->path, source, resolved, digest);
|
||||
free(source);
|
||||
return work;
|
||||
}
|
||||
|
||||
static double cx_import_shape_ratio(const CxImportShape *shape, int lines, uint64_t *base) {
|
||||
int resolved[2];
|
||||
uint64_t digest[2];
|
||||
uint64_t first = cx_import_shape_steps(shape, lines, &resolved[0], &digest[0]);
|
||||
uint64_t second = cx_import_shape_steps(shape, lines * 2, &resolved[1], &digest[1]);
|
||||
double ratio = cx_ratio((double)second, (double)first);
|
||||
printf(" %s lsp steps %llu -> %llu (ratio %.2f); resolved %d/%016llx, %d/%016llx\n",
|
||||
shape->path, (unsigned long long)first, (unsigned long long)second, ratio, resolved[0],
|
||||
(unsigned long long)digest[0], resolved[1], (unsigned long long)digest[1]);
|
||||
*base = first;
|
||||
return ratio;
|
||||
}
|
||||
|
||||
TEST(complexity_go_import_uses_are_linear) {
|
||||
static const CxImportShape shape = {CBM_LANG_GO,
|
||||
"uses.go",
|
||||
"package p\nimport (\n",
|
||||
"\tq%d \"example.com/q%d\"\n",
|
||||
")\nfunc F() {\n",
|
||||
"\tq%d.G()\n",
|
||||
"}\n"};
|
||||
uint64_t base = 0;
|
||||
double ratio = cx_import_shape_ratio(&shape, CX_BINDING_LINES, &base);
|
||||
ASSERT_GTE(base, (uint64_t)CX_BINDING_LINES);
|
||||
ASSERT_TRUE(ratio >= CX_RATIO_LO && ratio <= CX_RATIO_HI);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* Go's own pass looked for each method's receiver among all the file's types, and
|
||||
* patched each alias, interface and struct after a scan of the registry. */
|
||||
static const char *cx_line_go_types(char *out, size_t cap, int i) {
|
||||
snprintf(out, cap,
|
||||
"type T%d struct{ A int }\ntype U%d = T%d\ntype I%d interface{ M() int }\n"
|
||||
"func (t T%d) M() int { return t.A }\n",
|
||||
i, i, i, i, i);
|
||||
return out;
|
||||
}
|
||||
TEST(complexity_go_pass_registration_is_linear) {
|
||||
uint64_t base = 0;
|
||||
double ratio =
|
||||
cx_pass_ratio(CBM_LANG_GO, "types.go", "package p\n", cx_line_go_types, "", &base);
|
||||
ASSERT_GTE(base, (uint64_t)CX_BINDING_LINES);
|
||||
ASSERT_TRUE(ratio >= CX_RATIO_LO && ratio <= CX_RATIO_HI);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* Each generic function patched the first registration of its name after a scan. */
|
||||
static const char *cx_line_go_generic(char *out, size_t cap, int i) {
|
||||
snprintf(out, cap, "func F%d[T any](a T) T { return a }\n", i);
|
||||
return out;
|
||||
}
|
||||
TEST(complexity_go_generic_registration_is_linear) {
|
||||
uint64_t base = 0;
|
||||
double ratio =
|
||||
cx_pass_ratio(CBM_LANG_GO, "generic.go", "package p\n", cx_line_go_generic, "", &base);
|
||||
ASSERT_GTE(base, (uint64_t)CX_BINDING_LINES);
|
||||
ASSERT_TRUE(ratio >= CX_RATIO_LO && ratio <= CX_RATIO_HI);
|
||||
PASS();
|
||||
}
|
||||
|
||||
TEST(complexity_java_import_uses_are_linear) {
|
||||
static const CxImportShape shape = {CBM_LANG_JAVA,
|
||||
"Uses.java",
|
||||
"",
|
||||
"import p.q%d.R%d;\n",
|
||||
"public class Uses { void f() {\n",
|
||||
" R%d.call();\n",
|
||||
"} }\n"};
|
||||
uint64_t base = 0;
|
||||
double ratio = cx_import_shape_ratio(&shape, CX_BINDING_LINES, &base);
|
||||
ASSERT_GTE(base, (uint64_t)CX_BINDING_LINES);
|
||||
ASSERT_TRUE(ratio >= CX_RATIO_LO && ratio <= CX_RATIO_HI);
|
||||
PASS();
|
||||
}
|
||||
/* Each call also does work that does not grow with the file (it asks the standard
|
||||
* library), so the static imports' scan only dominates at a larger size. */
|
||||
TEST(complexity_java_static_import_uses_are_linear) {
|
||||
enum { STATIC_IMPORT_LINES = 4500 };
|
||||
static const CxImportShape shape = {CBM_LANG_JAVA,
|
||||
"Statics.java",
|
||||
"",
|
||||
"import static p.Q%d.m%d;\n",
|
||||
"public class Statics { void f() {\n",
|
||||
" m%d();\n",
|
||||
"} }\n"};
|
||||
uint64_t base = 0;
|
||||
double ratio = cx_import_shape_ratio(&shape, STATIC_IMPORT_LINES, &base);
|
||||
ASSERT_GTE(base, (uint64_t)STATIC_IMPORT_LINES);
|
||||
ASSERT_TRUE(ratio >= CX_RATIO_LO && ratio <= CX_RATIO_HI);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* Java's own pass looked for each class among all registered types to fill its
|
||||
* bases and fields, and for each method among all registered functions. */
|
||||
static const char *cx_line_java_class(char *out, size_t cap, int i) {
|
||||
snprintf(out, cap, "class C%d extends B { int f; void m(int a) { } }\n", i);
|
||||
return out;
|
||||
}
|
||||
TEST(complexity_java_class_registration_is_linear) {
|
||||
uint64_t base = 0;
|
||||
double ratio = cx_pass_ratio(CBM_LANG_JAVA, "Classes.java", "", cx_line_java_class, "", &base);
|
||||
ASSERT_GTE(base, (uint64_t)CX_BINDING_LINES);
|
||||
ASSERT_TRUE(ratio >= CX_RATIO_LO && ratio <= CX_RATIO_HI);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* A type name no import explains looked through every registered type. */
|
||||
static const char *cx_line_java_unknown_type(char *out, size_t cap, int i) {
|
||||
snprintf(out, cap, "class A%d { Unknown%d field; Unknown%d get() { return field; } }\n", i, i,
|
||||
i);
|
||||
return out;
|
||||
}
|
||||
TEST(complexity_java_short_name_fallback_is_linear) {
|
||||
uint64_t base = 0;
|
||||
double ratio =
|
||||
cx_pass_ratio(CBM_LANG_JAVA, "Types.java", "", cx_line_java_unknown_type, "", &base);
|
||||
ASSERT_GTE(base, (uint64_t)CX_BINDING_LINES);
|
||||
ASSERT_TRUE(ratio >= CX_RATIO_LO && ratio <= CX_RATIO_HI);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* The tail index must answer what the scan answers, duplicates and later indexes included. */
|
||||
static int cx_type_row(const CBMTypeRegistry *reg, const char *name) {
|
||||
const CBMRegisteredType *found = cbm_registry_lookup_type(reg, name);
|
||||
return found ? (int)(found - reg->types) : -1;
|
||||
}
|
||||
static int cx_func_row(const CBMTypeRegistry *reg, const char *name) {
|
||||
const CBMRegisteredFunc *found = cbm_registry_lookup_func(reg, name);
|
||||
return found ? (int)(found - reg->funcs) : -1;
|
||||
}
|
||||
TEST(complexity_registry_tail_answers_as_a_scan_does) {
|
||||
enum { ROWS = 600, NAMES = 250, INDEX_AT = 200, PROBE_STEP = 37 };
|
||||
CBMArena arena, tail_arena, idx_arena;
|
||||
cbm_arena_init(&arena);
|
||||
cbm_arena_init(&tail_arena);
|
||||
cbm_arena_init(&idx_arena);
|
||||
CBMTypeRegistry with, without;
|
||||
cbm_registry_init(&with, &arena);
|
||||
cbm_registry_init(&without, &arena);
|
||||
with.tail_arena = &tail_arena;
|
||||
bool memo_used = false;
|
||||
bool func_memo_used = false;
|
||||
for (int i = 0; i < ROWS; i++) {
|
||||
char name[CX_BINDING_LINE_ROOM];
|
||||
snprintf(name, sizeof(name), "t%d", i % NAMES);
|
||||
const char *qn = cbm_arena_strdup(&arena, name);
|
||||
CBMRegisteredType type = {.qualified_name = qn, .short_name = qn};
|
||||
CBMRegisteredFunc func = {.qualified_name = qn, .short_name = qn};
|
||||
cbm_registry_add_type(&with, type);
|
||||
cbm_registry_add_type(&without, type);
|
||||
cbm_registry_add_func(&with, func);
|
||||
cbm_registry_add_func(&without, func);
|
||||
if (i == INDEX_AT) {
|
||||
cbm_registry_finalize_into(&with, &idx_arena);
|
||||
cbm_registry_finalize_into(&without, &idx_arena);
|
||||
}
|
||||
memo_used = memo_used || with.type_tail.synced > with.type_tail.start;
|
||||
func_memo_used = func_memo_used || with.func_tail.synced > with.func_tail.start;
|
||||
for (int probe = 0; probe < NAMES + PROBE_STEP; probe += PROBE_STEP) {
|
||||
char ask[CX_BINDING_LINE_ROOM];
|
||||
snprintf(ask, sizeof(ask), "t%d", probe);
|
||||
ASSERT_EQ(cx_type_row(&with, ask), cx_type_row(&without, ask));
|
||||
ASSERT_EQ(cx_func_row(&with, ask), cx_func_row(&without, ask));
|
||||
const CBMRegisteredType *own_with = cbm_registry_first_own_type(&with, ask);
|
||||
const CBMRegisteredType *own_without = cbm_registry_first_own_type(&without, ask);
|
||||
ASSERT_EQ(own_with ? (int)(own_with - with.types) : -1,
|
||||
own_without ? (int)(own_without - without.types) : -1);
|
||||
const CBMRegisteredFunc *fn_with = cbm_registry_first_own_func(&with, ask);
|
||||
const CBMRegisteredFunc *fn_without = cbm_registry_first_own_func(&without, ask);
|
||||
ASSERT_EQ(fn_with ? (int)(fn_with - with.funcs) : -1,
|
||||
fn_without ? (int)(fn_without - without.funcs) : -1);
|
||||
}
|
||||
}
|
||||
ASSERT_TRUE(memo_used);
|
||||
ASSERT_TRUE(func_memo_used);
|
||||
cbm_arena_destroy(&idx_arena);
|
||||
cbm_arena_destroy(&tail_arena);
|
||||
cbm_arena_destroy(&arena);
|
||||
PASS();
|
||||
}
|
||||
TEST(complexity_kotlin_import_uses_are_linear) {
|
||||
static const CxImportShape shape = {
|
||||
CBM_LANG_KOTLIN, "uses.kt", "package p\n", "import q.r%d.S%d\n",
|
||||
"fun f() {\n", " S%d.call()\n", "}\n"};
|
||||
uint64_t base = 0;
|
||||
double ratio = cx_import_shape_ratio(&shape, CX_BINDING_LINES, &base);
|
||||
ASSERT_GTE(base, (uint64_t)CX_BINDING_LINES);
|
||||
ASSERT_TRUE(ratio >= CX_RATIO_LO && ratio <= CX_RATIO_HI);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* A file of classes: each one's registration looked at all that were registered before. */
|
||||
static const char *cx_line_kotlin_class(char *out, size_t cap, int i) {
|
||||
snprintf(out, cap, "class C%d { val x: Int = 1\n fun f(a: Int): Int = a }\n", i);
|
||||
return out;
|
||||
}
|
||||
TEST(complexity_kotlin_class_registration_is_linear) {
|
||||
uint64_t base = 0;
|
||||
double ratio = cx_pass_ratio(CBM_LANG_KOTLIN, "classes.kt", "package p\n", cx_line_kotlin_class,
|
||||
"", &base);
|
||||
ASSERT_GTE(base, (uint64_t)CX_BINDING_LINES);
|
||||
ASSERT_TRUE(ratio >= CX_RATIO_LO && ratio <= CX_RATIO_HI);
|
||||
PASS();
|
||||
}
|
||||
|
||||
TEST(complexity_ts_import_uses_are_linear) {
|
||||
static const CxImportShape typescript = {
|
||||
CBM_LANG_TYPESCRIPT, "uses.ts", "", "import { n_%d } from './m_%d';\n", "",
|
||||
"n_%d();\n", ""};
|
||||
static const CxImportShape javascript = {
|
||||
CBM_LANG_JAVASCRIPT, "uses.js", "", "import { n_%d } from './m_%d.js';\n", "",
|
||||
"n_%d();\n", ""};
|
||||
uint64_t ts_base = 0;
|
||||
uint64_t js_base = 0;
|
||||
double ts_ratio = cx_import_shape_ratio(&typescript, CX_BINDING_LINES, &ts_base);
|
||||
double js_ratio = cx_import_shape_ratio(&javascript, CX_BINDING_LINES, &js_base);
|
||||
ASSERT_GTE(ts_base, (uint64_t)CX_BINDING_LINES);
|
||||
ASSERT_GTE(js_base, (uint64_t)CX_BINDING_LINES);
|
||||
ASSERT_TRUE(ts_ratio >= CX_RATIO_LO && ts_ratio <= CX_RATIO_HI);
|
||||
ASSERT_TRUE(js_ratio >= CX_RATIO_LO && js_ratio <= CX_RATIO_HI);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* TypeScript's own pass listed each type's methods by scanning every function,
|
||||
* and patched each class, alias, JSDoc signature and inferred return after a
|
||||
* scan of the registry. */
|
||||
static const char *cx_line_ts_class(char *out, size_t cap, int i) {
|
||||
snprintf(out, cap,
|
||||
"export class C%d { x: number = 1; m(a: number): number { return a; } }\n"
|
||||
"type A%d = C%d;\n",
|
||||
i, i, i);
|
||||
return out;
|
||||
}
|
||||
TEST(complexity_ts_class_registration_is_linear) {
|
||||
uint64_t base = 0;
|
||||
double ratio =
|
||||
cx_pass_ratio(CBM_LANG_TYPESCRIPT, "classes.ts", "", cx_line_ts_class, "", &base);
|
||||
ASSERT_GTE(base, (uint64_t)CX_BINDING_LINES);
|
||||
ASSERT_TRUE(ratio >= CX_RATIO_LO && ratio <= CX_RATIO_HI);
|
||||
PASS();
|
||||
}
|
||||
static const char *cx_line_js_function_pair(char *out, size_t cap, int i) {
|
||||
snprintf(out, cap,
|
||||
"/** @returns {number} */\nfunction f_%d(a) { return g_%d(a); }\n"
|
||||
"function g_%d(b) { return b; }\n",
|
||||
i, i, i);
|
||||
return out;
|
||||
}
|
||||
TEST(complexity_js_pass_registry_lookups_are_linear) {
|
||||
uint64_t base = 0;
|
||||
double ratio = cx_pass_ratio(CBM_LANG_JAVASCRIPT, "pairs.js", "export {};\n",
|
||||
cx_line_js_function_pair, "", &base);
|
||||
ASSERT_GTE(base, (uint64_t)CX_BINDING_LINES);
|
||||
ASSERT_TRUE(ratio >= CX_RATIO_LO && ratio <= CX_RATIO_HI);
|
||||
PASS();
|
||||
}
|
||||
|
||||
TEST(complexity_rust_import_uses_are_linear) {
|
||||
static const CxImportShape shape = {
|
||||
CBM_LANG_RUST, "uses.rs", "", "use crate::m%d::f%d;\n",
|
||||
"fn run() {\n", " f%d();\n", "}\n"};
|
||||
uint64_t base = 0;
|
||||
double ratio = cx_import_shape_ratio(&shape, CX_BINDING_LINES, &base);
|
||||
ASSERT_GTE(base, (uint64_t)CX_BINDING_LINES);
|
||||
ASSERT_TRUE(ratio >= CX_RATIO_LO && ratio <= CX_RATIO_HI);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* Rust's registry build patched each trait, impl method and free function after a
|
||||
* scan of every registered type or function. */
|
||||
static const char *cx_line_rust_items(char *out, size_t cap, int i) {
|
||||
snprintf(out, cap,
|
||||
"pub trait T%d { fn m(&self); }\nimpl S%d { pub fn n(&self) -> i32 { 1 } }\n"
|
||||
"fn f%d() -> i32 { 1 }\n",
|
||||
i, i, i);
|
||||
return out;
|
||||
}
|
||||
TEST(complexity_rust_pass_registration_is_linear) {
|
||||
uint64_t base = 0;
|
||||
double ratio = cx_pass_ratio(CBM_LANG_RUST, "items.rs", "", cx_line_rust_items, "", &base);
|
||||
ASSERT_GTE(base, (uint64_t)CX_BINDING_LINES);
|
||||
ASSERT_TRUE(ratio >= CX_RATIO_LO && ratio <= CX_RATIO_HI);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* Each derive and each struct's fields looked for their type among all registered. */
|
||||
static const char *cx_line_rust_derive(char *out, size_t cap, int i) {
|
||||
snprintf(out, cap, "#[derive(Clone, Debug)]\npub struct S%d { pub a: i32 }\n", i);
|
||||
return out;
|
||||
}
|
||||
TEST(complexity_rust_derive_registration_is_linear) {
|
||||
uint64_t base = 0;
|
||||
double ratio = cx_pass_ratio(CBM_LANG_RUST, "derive.rs", "", cx_line_rust_derive, "", &base);
|
||||
ASSERT_GTE(base, (uint64_t)CX_BINDING_LINES);
|
||||
ASSERT_TRUE(ratio >= CX_RATIO_LO && ratio <= CX_RATIO_HI);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* C#'s own pass patched each method's parameters and return type, and each class's
|
||||
* fields, after a scan of every registered function or type. Every definition also
|
||||
* does work of its own, so the scans show at this size. */
|
||||
static const char *cx_line_csharp_methods(char *out, size_t cap, int i) {
|
||||
snprintf(out, cap,
|
||||
"class C%d { public int X; int F(int a) { return a; } int G() { return 1; } "
|
||||
"int H() { return 1; } }\n",
|
||||
i);
|
||||
return out;
|
||||
}
|
||||
TEST(complexity_csharp_pass_registration_is_linear) {
|
||||
enum { CSHARP_LINES = 3000 };
|
||||
int resolved[2];
|
||||
uint64_t digest[2];
|
||||
uint64_t first = cx_extract_lsp_steps(CBM_LANG_CSHARP, "methods.cs", "", cx_line_csharp_methods,
|
||||
"", CSHARP_LINES, &resolved[0], &digest[0]);
|
||||
uint64_t second =
|
||||
cx_extract_lsp_steps(CBM_LANG_CSHARP, "methods.cs", "", cx_line_csharp_methods, "",
|
||||
CSHARP_LINES * 2, &resolved[1], &digest[1]);
|
||||
double ratio = cx_ratio((double)second, (double)first);
|
||||
printf(" methods.cs lsp steps %llu -> %llu (ratio %.2f); resolved %d/%016llx, %d/%016llx\n",
|
||||
(unsigned long long)first, (unsigned long long)second, ratio, resolved[0],
|
||||
(unsigned long long)digest[0], resolved[1], (unsigned long long)digest[1]);
|
||||
ASSERT_GTE(first, (uint64_t)CSHARP_LINES);
|
||||
ASSERT_TRUE(ratio >= CX_RATIO_LO && ratio <= CX_RATIO_HI);
|
||||
PASS();
|
||||
}
|
||||
|
||||
TEST(complexity_php_import_uses_are_linear) {
|
||||
static const CxImportShape shape = {CBM_LANG_PHP,
|
||||
"uses.php",
|
||||
"<?php\nnamespace App;\n",
|
||||
"use Lib\\A%d;\n",
|
||||
"function f() {\n",
|
||||
" A%d::go();\n",
|
||||
"}\n"};
|
||||
uint64_t base = 0;
|
||||
double ratio = cx_import_shape_ratio(&shape, CX_BINDING_LINES, &base);
|
||||
ASSERT_GTE(base, (uint64_t)CX_BINDING_LINES);
|
||||
ASSERT_TRUE(ratio >= CX_RATIO_LO && ratio <= CX_RATIO_HI);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* A call to a name nothing declares looked through every registered function. */
|
||||
static const char *cx_line_php_unknown_call(char *out, size_t cap, int i) {
|
||||
snprintf(out, cap, "function f_%d() { return g_%d(); }\n", i, i);
|
||||
return out;
|
||||
}
|
||||
TEST(complexity_php_short_name_fallback_is_linear) {
|
||||
uint64_t base = 0;
|
||||
double ratio =
|
||||
cx_pass_ratio(CBM_LANG_PHP, "calls.php", "<?php\n", cx_line_php_unknown_call, "", &base);
|
||||
ASSERT_GTE(base, (uint64_t)CX_BINDING_LINES);
|
||||
ASSERT_TRUE(ratio >= CX_RATIO_LO && ratio <= CX_RATIO_HI);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* Each method's declared or documented return type patched its registration after
|
||||
* a scan of every registered function. The classes sit in namespace blocks of a
|
||||
* hundred: the class walk's fixed stack skips the children of a node wider than
|
||||
* it holds (php_lsp_collect_class_fields), so one flat list would not grow. */
|
||||
enum { CX_PHP_CLASS_GROUP = 100 };
|
||||
static const char *cx_line_php_returns(char *out, size_t cap, int i) {
|
||||
int at = 0;
|
||||
if (i > 0 && i % CX_PHP_CLASS_GROUP == 0)
|
||||
at = snprintf(out, cap, "}\nnamespace N%d {\n", i / CX_PHP_CLASS_GROUP);
|
||||
snprintf(out + at, cap - (size_t)at,
|
||||
"class C%d { function m(): int { return 1; }\n/** @return int */\n"
|
||||
"function n() { return 1; } }\n",
|
||||
i);
|
||||
return out;
|
||||
}
|
||||
TEST(complexity_php_method_returns_are_linear) {
|
||||
uint64_t base = 0;
|
||||
double ratio = cx_pass_ratio(CBM_LANG_PHP, "returns.php", "<?php\nnamespace N0 {\n",
|
||||
cx_line_php_returns, "}\n", &base);
|
||||
ASSERT_GTE(base, (uint64_t)CX_BINDING_LINES);
|
||||
ASSERT_TRUE(ratio >= CX_RATIO_LO && ratio <= CX_RATIO_HI);
|
||||
PASS();
|
||||
}
|
||||
|
||||
SUITE(complexity) {
|
||||
RUN_TEST(complexity_py_module_ranges_preserve_calls_with_bounded_work);
|
||||
RUN_TEST(complexity_rescript_error_recovery_lexing_is_linear);
|
||||
@@ -1922,4 +2469,27 @@ SUITE(complexity) {
|
||||
RUN_TEST(complexity_spawn_match_is_linear_in_a_files_imports);
|
||||
RUN_TEST(complexity_spawn_file_index_answers_as_the_rows_do);
|
||||
RUN_TEST(complexity_go_route_bindings_are_near_linear);
|
||||
RUN_TEST(complexity_name_rows_answer_as_a_scan_does);
|
||||
RUN_TEST(complexity_python_pass_import_join_is_linear);
|
||||
RUN_TEST(complexity_python_pass_wide_from_import_is_linear);
|
||||
RUN_TEST(complexity_go_import_uses_are_linear);
|
||||
RUN_TEST(complexity_go_pass_registration_is_linear);
|
||||
RUN_TEST(complexity_go_generic_registration_is_linear);
|
||||
RUN_TEST(complexity_java_import_uses_are_linear);
|
||||
RUN_TEST(complexity_java_static_import_uses_are_linear);
|
||||
RUN_TEST(complexity_java_class_registration_is_linear);
|
||||
RUN_TEST(complexity_java_short_name_fallback_is_linear);
|
||||
RUN_TEST(complexity_registry_tail_answers_as_a_scan_does);
|
||||
RUN_TEST(complexity_kotlin_import_uses_are_linear);
|
||||
RUN_TEST(complexity_kotlin_class_registration_is_linear);
|
||||
RUN_TEST(complexity_ts_import_uses_are_linear);
|
||||
RUN_TEST(complexity_ts_class_registration_is_linear);
|
||||
RUN_TEST(complexity_js_pass_registry_lookups_are_linear);
|
||||
RUN_TEST(complexity_rust_import_uses_are_linear);
|
||||
RUN_TEST(complexity_rust_pass_registration_is_linear);
|
||||
RUN_TEST(complexity_rust_derive_registration_is_linear);
|
||||
RUN_TEST(complexity_csharp_pass_registration_is_linear);
|
||||
RUN_TEST(complexity_php_import_uses_are_linear);
|
||||
RUN_TEST(complexity_php_short_name_fallback_is_linear);
|
||||
RUN_TEST(complexity_php_method_returns_are_linear);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user