mirror of
https://github.com/DeusData/codebase-memory-mcp.git
synced 2026-12-02 04:17:04 +08:00
Merge pull request #2981 from DeusData/perf/linear-growth-sites
fix, perf: C# operator nodes, C increment writes and linear growth in extraction and eight language passes
This commit is contained in:
+2
-1
@@ -428,7 +428,8 @@ EXTRACTION_SRCS = \
|
||||
$(CBM_DIR)/macro_table.c \
|
||||
$(CBM_DIR)/iris_export_xml.c \
|
||||
$(CBM_DIR)/service_patterns.c \
|
||||
$(CBM_DIR)/spawn_patterns.c
|
||||
$(CBM_DIR)/spawn_patterns.c \
|
||||
$(CBM_DIR)/resolve_stats.c
|
||||
|
||||
# LSP resolvers (compiled as one unit via lsp_all.c)
|
||||
LSP_SRCS = $(CBM_DIR)/lsp_all.c
|
||||
|
||||
@@ -1137,6 +1137,14 @@ typedef struct {
|
||||
* The core never reads, interprets or frees it. It is the only place for
|
||||
* such state: a static or thread-local cache is not. */
|
||||
void *doclink_state;
|
||||
/* C#: the first named file-scoped namespace among the children of the
|
||||
* tree top cs_file_ns_top (extract_defs.c), found once per file. */
|
||||
const void *cs_file_ns_top;
|
||||
const char *cs_file_ns_name;
|
||||
uint32_t cs_file_ns_start;
|
||||
/* Go: what each block's statements bind (extract_calls.c), NULL until a route call asks;
|
||||
* allocated in `scratch`. */
|
||||
void *go_block_bindings;
|
||||
} CBMExtractCtx;
|
||||
|
||||
/* Internal configured-definition seams. No declarations pointer enters a result. */
|
||||
|
||||
+227
-14
@@ -14,6 +14,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include "resolve_stats.h"
|
||||
#if defined(CBM_KOTLIN_DEDUP_TEST_API) && CBM_KOTLIN_DEDUP_TEST_API
|
||||
#include <stdatomic.h>
|
||||
#endif
|
||||
@@ -2148,30 +2149,238 @@ static bool go_is_function_boundary(const char *k) {
|
||||
strcmp(k, "func_literal") == 0 || strcmp(k, "source_file") == 0;
|
||||
}
|
||||
|
||||
/* Latest binding of `name` among the statements preceding `use` in its
|
||||
* enclosing blocks, innermost block first; a null node when none. */
|
||||
static TSNode go_find_binding(CBMArena *a, TSNode use, const char *name, const char *source) {
|
||||
/* A function of route registrations asks what each receiver is bound to, so one block's bindings
|
||||
* are read once: every name each statement binds, by name and then in source order. A statement
|
||||
* that names a variable but gives it no value has no row, as go_stmt_binding has no answer. */
|
||||
typedef struct {
|
||||
const char *name;
|
||||
uint32_t statement_start;
|
||||
uint32_t order;
|
||||
TSNode value;
|
||||
} go_binding_t;
|
||||
|
||||
typedef struct {
|
||||
const void *block;
|
||||
go_binding_t *rows;
|
||||
size_t count, capacity;
|
||||
} go_block_bindings_t;
|
||||
|
||||
typedef struct {
|
||||
go_block_bindings_t **slots;
|
||||
size_t capacity, used;
|
||||
} go_bindings_table_t;
|
||||
|
||||
static bool go_binding_add(CBMArena *a, go_block_bindings_t *block, TSNode statement,
|
||||
const char *name, const char *source) {
|
||||
TSNode value = go_stmt_binding(a, statement, name, source);
|
||||
if (ts_node_is_null(value)) {
|
||||
return true;
|
||||
}
|
||||
if (block->count == block->capacity) {
|
||||
size_t capacity = block->capacity ? block->capacity * 2 : 16;
|
||||
go_binding_t *rows = cbm_arena_alloc(a, capacity * sizeof(*rows));
|
||||
if (!rows) {
|
||||
return false;
|
||||
}
|
||||
if (block->count) {
|
||||
memcpy(rows, block->rows, block->count * sizeof(*rows));
|
||||
}
|
||||
block->rows = rows;
|
||||
block->capacity = capacity;
|
||||
}
|
||||
block->rows[block->count] = (go_binding_t){.name = name,
|
||||
.statement_start = ts_node_start_byte(statement),
|
||||
.order = (uint32_t)block->count,
|
||||
.value = value};
|
||||
block->count++;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Every name a `var` statement declares, through its specs and their lists. */
|
||||
static bool go_var_names_add(CBMArena *a, go_block_bindings_t *block, TSNode statement, TSNode list,
|
||||
const char *source) {
|
||||
uint32_t count = ts_node_named_child_count(list);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
TSNode child = ts_node_named_child(list, i);
|
||||
const char *kind = ts_node_type(child);
|
||||
if (strcmp(kind, "var_spec") == 0) {
|
||||
uint32_t parts = ts_node_named_child_count(child);
|
||||
for (uint32_t p = 0; p < parts; p++) {
|
||||
TSNode part = ts_node_named_child(child, p);
|
||||
const char *name = strcmp(ts_node_type(part), "identifier") == 0
|
||||
? cbm_node_text(a, part, source)
|
||||
: NULL;
|
||||
if (name && !go_binding_add(a, block, statement, name, source)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else if (strcmp(kind, "var_spec_list") == 0 &&
|
||||
!go_var_names_add(a, block, statement, child, source)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool go_statement_names_add(CBMArena *a, go_block_bindings_t *block, TSNode statement,
|
||||
const char *source) {
|
||||
const char *kind = ts_node_type(statement);
|
||||
if (strcmp(kind, "var_declaration") == 0 || strcmp(kind, "var_spec_list") == 0) {
|
||||
return go_var_names_add(a, block, statement, statement, source);
|
||||
}
|
||||
if (strcmp(kind, "short_var_declaration") != 0 && strcmp(kind, "assignment_statement") != 0) {
|
||||
return true;
|
||||
}
|
||||
TSNode left = ts_node_child_by_field_name(statement, TS_FIELD("left"));
|
||||
uint32_t count = ts_node_is_null(left) ? 0 : ts_node_named_child_count(left);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
const char *name = cbm_node_text(a, ts_node_named_child(left, i), source);
|
||||
if (name && !go_binding_add(a, block, statement, name, source)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static int go_binding_order(const void *left, const void *right) {
|
||||
const go_binding_t *a = left, *b = right;
|
||||
int order = strcmp(a->name, b->name);
|
||||
if (order) {
|
||||
return order;
|
||||
}
|
||||
return a->order < b->order ? -1 : a->order > b->order;
|
||||
}
|
||||
|
||||
static go_block_bindings_t *go_block_bindings_build(CBMArena *a, TSNode node, const char *source) {
|
||||
go_block_bindings_t *block = cbm_arena_alloc(a, sizeof(*block));
|
||||
if (!block) {
|
||||
return NULL;
|
||||
}
|
||||
*block = (go_block_bindings_t){.block = node.id};
|
||||
bool ok = true;
|
||||
TSTreeCursor cursor = ts_tree_cursor_new(node);
|
||||
if (ts_tree_cursor_goto_first_child(&cursor)) {
|
||||
do {
|
||||
TSNode statement = ts_tree_cursor_current_node(&cursor);
|
||||
cbm_resolve_stats_add(CBM_RESOLVE_STAT_LOOKUP_PROBES, 1);
|
||||
ok =
|
||||
!ts_node_is_named(statement) || go_statement_names_add(a, block, statement, source);
|
||||
} while (ok && ts_tree_cursor_goto_next_sibling(&cursor));
|
||||
}
|
||||
ts_tree_cursor_delete(&cursor);
|
||||
if (!ok) {
|
||||
return NULL;
|
||||
}
|
||||
if (block->count > 1) {
|
||||
qsort(block->rows, block->count, sizeof(*block->rows), go_binding_order);
|
||||
}
|
||||
return block;
|
||||
}
|
||||
|
||||
static size_t go_block_hash(const void *id, size_t capacity) {
|
||||
uint64_t value = (uint64_t)(uintptr_t)id >> 3;
|
||||
value *= UINT64_C(0x9E3779B97F4A7C15);
|
||||
return (size_t)(value ^ (value >> 29)) & (capacity - 1);
|
||||
}
|
||||
|
||||
/* This block's bindings, read the first time anything in it asks. */
|
||||
static go_block_bindings_t *go_block_bindings_of(CBMExtractCtx *ctx, TSNode node) {
|
||||
enum { GO_BLOCKS_FIRST = 16 };
|
||||
CBMArena *a = ctx->scratch ? ctx->scratch : ctx->arena;
|
||||
go_bindings_table_t *table = ctx->go_block_bindings;
|
||||
if (!table) {
|
||||
table = cbm_arena_calloc(a, sizeof(*table));
|
||||
go_block_bindings_t **slots =
|
||||
table ? cbm_arena_calloc(a, GO_BLOCKS_FIRST * sizeof(*slots)) : NULL;
|
||||
if (!slots) {
|
||||
return NULL;
|
||||
}
|
||||
table->slots = slots;
|
||||
table->capacity = GO_BLOCKS_FIRST;
|
||||
ctx->go_block_bindings = table;
|
||||
}
|
||||
size_t at = go_block_hash(node.id, table->capacity);
|
||||
while (table->slots[at]) {
|
||||
if (table->slots[at]->block == node.id) {
|
||||
return table->slots[at];
|
||||
}
|
||||
at = (at + 1) & (table->capacity - 1);
|
||||
}
|
||||
go_block_bindings_t *block = go_block_bindings_build(a, node, ctx->source);
|
||||
if (!block) {
|
||||
return NULL;
|
||||
}
|
||||
table->slots[at] = block;
|
||||
table->used++;
|
||||
if (table->used * 2 > table->capacity) {
|
||||
size_t capacity = table->capacity * 2;
|
||||
go_block_bindings_t **slots = cbm_arena_calloc(a, capacity * sizeof(*slots));
|
||||
if (!slots) {
|
||||
return NULL;
|
||||
}
|
||||
for (size_t i = 0; i < table->capacity; i++) {
|
||||
if (table->slots[i]) {
|
||||
size_t place = go_block_hash(table->slots[i]->block, capacity);
|
||||
while (slots[place]) {
|
||||
place = (place + 1) & (capacity - 1);
|
||||
}
|
||||
slots[place] = table->slots[i];
|
||||
}
|
||||
}
|
||||
table->slots = slots;
|
||||
table->capacity = capacity;
|
||||
}
|
||||
return block;
|
||||
}
|
||||
|
||||
/* The binding of `name` by the last statement in this block that starts before `before`. */
|
||||
static TSNode go_block_binding(const go_block_bindings_t *block, const char *name,
|
||||
uint32_t before) {
|
||||
size_t low = 0, high = block->count;
|
||||
while (low < high) {
|
||||
cbm_resolve_stats_add(CBM_RESOLVE_STAT_LOOKUP_PROBES, 1);
|
||||
size_t middle = low + (high - low) / 2;
|
||||
const go_binding_t *row = &block->rows[middle];
|
||||
int order = strcmp(row->name, name);
|
||||
if (order < 0 || (order == 0 && row->statement_start < before)) {
|
||||
low = middle + 1;
|
||||
} else {
|
||||
high = middle;
|
||||
}
|
||||
}
|
||||
if (low == 0 || strcmp(block->rows[low - 1].name, name) != 0) {
|
||||
return (TSNode){0};
|
||||
}
|
||||
return block->rows[low - 1].value;
|
||||
}
|
||||
|
||||
/* Latest binding of `name` among the statements preceding `use` in its enclosing blocks, innermost
|
||||
* block first; a null node when none. False when memory ran out and the answer is unknown. */
|
||||
static bool go_find_binding(CBMExtractCtx *ctx, TSNode use, const char *name, TSNode *binding) {
|
||||
*binding = (TSNode){0};
|
||||
TSNode child = use;
|
||||
TSNode cur = ts_node_parent(use);
|
||||
while (!ts_node_is_null(cur) && !go_is_function_boundary(ts_node_type(cur))) {
|
||||
const char *k = ts_node_type(cur);
|
||||
if (strcmp(k, "block") == 0 || strcmp(k, "statement_list") == 0) {
|
||||
for (TSNode s = ts_node_prev_named_sibling(child); !ts_node_is_null(s);
|
||||
s = ts_node_prev_named_sibling(s)) {
|
||||
TSNode v = go_stmt_binding(a, s, name, source);
|
||||
if (!ts_node_is_null(v)) {
|
||||
return v;
|
||||
}
|
||||
const go_block_bindings_t *block = go_block_bindings_of(ctx, cur);
|
||||
if (!block) {
|
||||
return false;
|
||||
}
|
||||
*binding = go_block_binding(block, name, ts_node_start_byte(child));
|
||||
if (!ts_node_is_null(*binding)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
child = cur;
|
||||
cur = ts_node_parent(cur);
|
||||
}
|
||||
TSNode none = {0};
|
||||
return none;
|
||||
return true;
|
||||
}
|
||||
|
||||
static const char *go_group_prefix_for_call(CBMArena *a, TSNode call, const char *source) {
|
||||
static const char *go_group_prefix_for_call(CBMExtractCtx *ctx, TSNode call) {
|
||||
CBMArena *a = ctx->arena;
|
||||
const char *source = ctx->source;
|
||||
TSNode fn = ts_node_child_by_field_name(call, TS_FIELD("function"));
|
||||
if (ts_node_is_null(fn) || strcmp(ts_node_type(fn), "selector_expression") != 0) {
|
||||
return NULL;
|
||||
@@ -2190,7 +2399,11 @@ static const char *go_group_prefix_for_call(CBMArena *a, TSNode call, const char
|
||||
}
|
||||
if (strcmp(k, "identifier") == 0) {
|
||||
const char *name = cbm_node_text(a, recv, source);
|
||||
recv = name ? go_find_binding(a, recv, name, source) : (TSNode){0};
|
||||
TSNode bound = {0};
|
||||
if (name && !go_find_binding(ctx, recv, name, &bound)) {
|
||||
return NULL; /* never a partial prefix */
|
||||
}
|
||||
recv = bound;
|
||||
continue; /* unbound (parameter, package var): root reached */
|
||||
}
|
||||
TSNode inner = {0};
|
||||
@@ -2223,7 +2436,7 @@ static const char *route_path_with_group_prefix(CBMExtractCtx *ctx, TSNode node,
|
||||
if (ctx->language == CBM_LANG_PHP) {
|
||||
gp = php_group_prefix_for_call(ctx->arena, node, ctx->source);
|
||||
} else if (ctx->language == CBM_LANG_GO) {
|
||||
gp = go_group_prefix_for_call(ctx->arena, node, ctx->source);
|
||||
gp = go_group_prefix_for_call(ctx, node);
|
||||
}
|
||||
return (gp && gp[0]) ? route_path_with_prefix(ctx->arena, gp, path) : path;
|
||||
}
|
||||
|
||||
+49
-18
@@ -11,6 +11,7 @@
|
||||
#include "foundation/sha256.h"
|
||||
#include "foundation/mem_core.h" // cbm_realloc/cbm_free -- walk_defs stack
|
||||
#include "extract_node_stack.h"
|
||||
#include "resolve_stats.h"
|
||||
#include "simhash/minhash.h"
|
||||
#include "semantic/ast_profile.h"
|
||||
#include "tree_sitter/api.h" // TSNode, ts_node_*
|
||||
@@ -771,6 +772,11 @@ TSNode cbm_resolve_func_name(TSNode node, CBMLanguage lang) {
|
||||
for (int tmpl_depth = 0; tmpl_depth < MAX_TEMPLATE_DEPTH; tmpl_depth++) {
|
||||
const char *kind = ts_node_type(node);
|
||||
|
||||
// C#: an overloaded operator has no name field, its token names it.
|
||||
if (lang == CBM_LANG_CSHARP && strcmp(kind, "operator_declaration") == 0) {
|
||||
return ts_node_child_by_field_name(node, TS_FIELD("operator"));
|
||||
}
|
||||
|
||||
if (lang == CBM_LANG_HASKELL && strcmp(kind, "signature") == 0) {
|
||||
TSNode null_node = {0};
|
||||
return null_node;
|
||||
@@ -1779,6 +1785,7 @@ static bool doc_memo_fill(CBMArena *a, doc_memo_ent_t *e, TSNode parent) {
|
||||
} while (k < n && ts_tree_cursor_goto_next_sibling(&cur));
|
||||
}
|
||||
ts_tree_cursor_delete(&cur);
|
||||
cbm_resolve_stats_add(CBM_RESOLVE_STAT_SYNTAX_CHILD_STEPS, k);
|
||||
e->node = parent;
|
||||
e->n = k;
|
||||
e->used = true;
|
||||
@@ -1884,6 +1891,10 @@ static TSNode doc_parent(CBMExtractCtx *ctx, TSNode node) {
|
||||
const doc_memo_ent_t *e = doc_memo_ancestor((const doc_memo_t *)ctx->doc_memo, node);
|
||||
if (!e) {
|
||||
e = doc_memo_get(ctx, ctx->root);
|
||||
} else {
|
||||
/* Use keeps it cached: a wide root evicted by its children's narrow lists is listed
|
||||
* again every few definitions. */
|
||||
((doc_memo_ent_t *)e)->stamp = ++((doc_memo_t *)ctx->doc_memo)->clock;
|
||||
}
|
||||
while (e) {
|
||||
int k = doc_memo_containing(e, node);
|
||||
@@ -8461,6 +8472,28 @@ static const char *cs_namespace_decl_name(CBMArena *a, TSNode ns, const char *so
|
||||
return text && text[0] ? text : NULL;
|
||||
}
|
||||
|
||||
/* Note the first named file-scoped namespace among top's named children, once per
|
||||
* file: asked per type, ts_node_named_child(top, i) steps over the i children
|
||||
* before it, and a file of n types paid that for every type. */
|
||||
static void cs_note_file_namespace(CBMExtractCtx *ctx, TSNode top) {
|
||||
ctx->cs_file_ns_top = top.id;
|
||||
ctx->cs_file_ns_name = NULL;
|
||||
ctx->cs_file_ns_start = 0;
|
||||
TSTreeCursor cur = ts_tree_cursor_new(top);
|
||||
bool more = ts_tree_cursor_goto_first_child(&cur);
|
||||
while (more && !ctx->cs_file_ns_name) {
|
||||
cbm_resolve_stats_add(CBM_RESOLVE_STAT_SYNTAX_CHILD_STEPS, 1);
|
||||
TSNode c = ts_tree_cursor_current_node(&cur);
|
||||
if (ts_node_is_named(c) &&
|
||||
strcmp(ts_node_type(c), "file_scoped_namespace_declaration") == 0) {
|
||||
ctx->cs_file_ns_name = cs_namespace_decl_name(ctx->arena, c, ctx->source);
|
||||
ctx->cs_file_ns_start = ts_node_start_byte(c);
|
||||
}
|
||||
more = ts_tree_cursor_goto_next_sibling(&cur);
|
||||
}
|
||||
ts_tree_cursor_delete(&cur);
|
||||
}
|
||||
|
||||
/* Declared namespace of a C# type declaration: every enclosing namespace
|
||||
* block (outer to inner) under an optional file-scoped namespace, which the
|
||||
* grammar may attach either as an ancestor or as a preceding sibling of the
|
||||
@@ -8473,7 +8506,9 @@ static const char *cs_namespace_decl_name(CBMArena *a, TSNode ns, const char *so
|
||||
* grammar does not know (a `ref partial struct` with members does it), leaving
|
||||
* the types after it parented straight under the compilation unit. Calling
|
||||
* those global would let them hide every imported type of the same name. */
|
||||
static const char *cs_type_decl_namespace(CBMArena *a, TSNode node, const char *source) {
|
||||
static const char *cs_type_decl_namespace(CBMExtractCtx *ctx, TSNode node) {
|
||||
CBMArena *a = ctx->arena;
|
||||
const char *source = ctx->source;
|
||||
const char *acc = NULL;
|
||||
bool file_scoped = false;
|
||||
TSNode top = node;
|
||||
@@ -8492,19 +8527,14 @@ static const char *cs_type_decl_namespace(CBMArena *a, TSNode node, const char *
|
||||
if (file_scoped) {
|
||||
return acc;
|
||||
}
|
||||
uint32_t start = ts_node_start_byte(node);
|
||||
uint32_t n = ts_node_named_child_count(top);
|
||||
for (uint32_t i = 0; i < n; i++) {
|
||||
TSNode c = ts_node_named_child(top, i);
|
||||
if (ts_node_start_byte(c) >= start) {
|
||||
break;
|
||||
}
|
||||
if (strcmp(ts_node_type(c), "file_scoped_namespace_declaration") == 0) {
|
||||
const char *name = cs_namespace_decl_name(a, c, source);
|
||||
if (name) {
|
||||
return acc ? cbm_arena_sprintf(a, "%s.%s", name, acc) : name;
|
||||
}
|
||||
}
|
||||
if (ctx->cs_file_ns_top != top.id) {
|
||||
cs_note_file_namespace(ctx, top);
|
||||
}
|
||||
/* Children come in source order: the first named one precedes this type exactly when
|
||||
* some named one does. */
|
||||
if (ctx->cs_file_ns_name && ctx->cs_file_ns_start < ts_node_start_byte(node)) {
|
||||
const char *name = ctx->cs_file_ns_name;
|
||||
return acc ? cbm_arena_sprintf(a, "%s.%s", name, acc) : name;
|
||||
}
|
||||
if (acc) {
|
||||
return acc;
|
||||
@@ -8913,7 +8943,7 @@ static void emit_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *s
|
||||
def.lines = (int)(def.end_line - def.start_line + TS_LINE_OFFSET);
|
||||
def.is_exported = cbm_is_exported(name, ctx->language);
|
||||
if (ctx->language == CBM_LANG_CSHARP && !ctx->enclosing_class_qn) {
|
||||
def.decl_namespace = cs_type_decl_namespace(a, node, ctx->source);
|
||||
def.decl_namespace = cs_type_decl_namespace(ctx, node);
|
||||
}
|
||||
if (ctx->language == CBM_LANG_SWIFT && ctx->module_qn) {
|
||||
TSNode kind_node = ts_node_child_by_field_name(node, TS_FIELD("declaration_kind"));
|
||||
@@ -9309,9 +9339,10 @@ static TSNode resolve_method_name(TSNode child, CBMLanguage lang) {
|
||||
|
||||
const char *ck = ts_node_type(child);
|
||||
|
||||
if ((lang == CBM_LANG_C || lang == CBM_LANG_CPP || lang == CBM_LANG_CUDA ||
|
||||
lang == CBM_LANG_GLSL) &&
|
||||
strcmp(ck, "function_definition") == 0) {
|
||||
if (((lang == CBM_LANG_C || lang == CBM_LANG_CPP || lang == CBM_LANG_CUDA ||
|
||||
lang == CBM_LANG_GLSL) &&
|
||||
strcmp(ck, "function_definition") == 0) ||
|
||||
(lang == CBM_LANG_CSHARP && strcmp(ck, "operator_declaration") == 0)) {
|
||||
return cbm_resolve_func_name(child, lang);
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ enum { MAX_INFRA_BINDINGS = 8 };
|
||||
#include <stdint.h> // uint32_t, uint8_t
|
||||
#include <string.h>
|
||||
#include <strings.h> // strcasecmp (ObjectScript type inference)
|
||||
#include "resolve_stats.h"
|
||||
|
||||
// --- Scope stack management ---
|
||||
|
||||
@@ -2590,18 +2591,50 @@ static TSNode nasm_wrapped_label(TSNode node, int remaining_depth) {
|
||||
return (TSNode){0};
|
||||
}
|
||||
|
||||
static TSNode nasm_preceding_label(TSNode node) {
|
||||
TSNode current = node;
|
||||
for (int level = 0; level < 4 && !ts_node_is_null(current); level++) {
|
||||
TSNode previous = ts_node_prev_named_sibling(current);
|
||||
while (!ts_node_is_null(previous)) {
|
||||
TSNode label = nasm_wrapped_label(previous, 2);
|
||||
if (!ts_node_is_null(label)) {
|
||||
return label;
|
||||
}
|
||||
previous = ts_node_prev_named_sibling(previous);
|
||||
/* The nearest earlier sibling a probe finds a label in. The walk reaches siblings in order, so a
|
||||
* search under the same parent stops at the sibling the last one started from, and what that one
|
||||
* found holds for everything before it: each sibling is probed once however many ask. The caller
|
||||
* passes node's parent, which it needs anyway. */
|
||||
typedef TSNode (*flat_label_probe_fn)(TSNode sibling);
|
||||
|
||||
static TSNode flat_preceding_label(CBMFlatLookback *memo, TSNode parent, TSNode node,
|
||||
flat_label_probe_fn probe) {
|
||||
bool known = memo->parent && memo->parent == parent.id;
|
||||
if (known && memo->asked == node.id) {
|
||||
return memo->found;
|
||||
}
|
||||
TSNode found = {0};
|
||||
for (TSNode previous = ts_node_prev_named_sibling(node); !ts_node_is_null(previous);
|
||||
previous = ts_node_prev_named_sibling(previous)) {
|
||||
cbm_resolve_stats_add(CBM_RESOLVE_STAT_LOOKUP_PROBES, 1);
|
||||
found = probe(previous);
|
||||
if (!ts_node_is_null(found)) {
|
||||
break;
|
||||
}
|
||||
if (known && memo->asked == previous.id) {
|
||||
found = memo->found;
|
||||
break;
|
||||
}
|
||||
}
|
||||
memo->parent = parent.id;
|
||||
memo->asked = node.id;
|
||||
memo->found = found;
|
||||
return found;
|
||||
}
|
||||
|
||||
static TSNode nasm_label_probe(TSNode sibling) {
|
||||
return nasm_wrapped_label(sibling, 2);
|
||||
}
|
||||
|
||||
static TSNode nasm_preceding_label(TSNode node, WalkState *state) {
|
||||
TSNode current = node;
|
||||
for (int level = 0; level < FLAT_LOOKBACK_LEVELS && !ts_node_is_null(current); level++) {
|
||||
TSNode parent = ts_node_parent(current);
|
||||
TSNode label =
|
||||
flat_preceding_label(&state->flat_lookback[level], parent, current, nasm_label_probe);
|
||||
if (!ts_node_is_null(label)) {
|
||||
return label;
|
||||
}
|
||||
if (ts_node_is_null(parent) || strcmp(ts_node_type(current), "source_file") == 0) {
|
||||
break;
|
||||
}
|
||||
@@ -2621,7 +2654,7 @@ static TSNode objectscript_routine_statement_tag(TSNode statement) {
|
||||
: cbm_find_child_by_kind(tag_statement, "tag");
|
||||
}
|
||||
|
||||
static TSNode objectscript_routine_preceding_tag(TSNode node) {
|
||||
static TSNode objectscript_routine_preceding_tag(TSNode node, WalkState *state) {
|
||||
if (strcmp(ts_node_type(node), "statement") != 0) {
|
||||
return (TSNode){0};
|
||||
}
|
||||
@@ -2631,14 +2664,8 @@ static TSNode objectscript_routine_preceding_tag(TSNode node) {
|
||||
!ts_node_is_null(cbm_find_child_by_kind(node, "procedure"))) {
|
||||
return (TSNode){0};
|
||||
}
|
||||
for (TSNode previous = ts_node_prev_named_sibling(node); !ts_node_is_null(previous);
|
||||
previous = ts_node_prev_named_sibling(previous)) {
|
||||
TSNode tag = objectscript_routine_statement_tag(previous);
|
||||
if (!ts_node_is_null(tag)) {
|
||||
return tag;
|
||||
}
|
||||
}
|
||||
return (TSNode){0};
|
||||
return flat_preceding_label(&state->flat_lookback[0], parent, node,
|
||||
objectscript_routine_statement_tag);
|
||||
}
|
||||
|
||||
static bool push_pre_node_scope(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec,
|
||||
@@ -2651,7 +2678,7 @@ static bool push_pre_node_scope(CBMExtractCtx *ctx, TSNode node, const CBMLangSp
|
||||
|
||||
TSNode label = {0};
|
||||
if (ctx->language == CBM_LANG_NASM && strcmp(ts_node_type(node), "actual_instruction") == 0) {
|
||||
label = nasm_preceding_label(node);
|
||||
label = nasm_preceding_label(node, state);
|
||||
} else if (ctx->language == CBM_LANG_OBJECTSCRIPT_ROUTINE) {
|
||||
if (strcmp(ts_node_type(node), "tag_statement") == 0) {
|
||||
/* A parameterized flat tag owns its header as well as the following
|
||||
@@ -2659,7 +2686,7 @@ static bool push_pre_node_scope(CBMExtractCtx *ctx, TSNode node, const CBMLangSp
|
||||
* parameter_list records lexical bindings under the same tag QN. */
|
||||
label = cbm_find_child_by_kind(node, "tag");
|
||||
} else {
|
||||
label = objectscript_routine_preceding_tag(node);
|
||||
label = objectscript_routine_preceding_tag(node, state);
|
||||
}
|
||||
}
|
||||
if (ts_node_is_null(label)) {
|
||||
@@ -3063,6 +3090,7 @@ void cbm_extract_unified(CBMExtractCtx *ctx) {
|
||||
CBMInvocationDescriptor invocation =
|
||||
tf_module ? (CBMInvocationDescriptor){0} : handle_calls(ctx, node, spec, &state);
|
||||
if (!tf_module) {
|
||||
state.walk_depth = depth;
|
||||
handle_usages(ctx, node, spec, &state);
|
||||
}
|
||||
handle_throws(ctx, node, spec, &state);
|
||||
|
||||
@@ -20,6 +20,14 @@
|
||||
#define INLINE_PYTHON_DIRECTIVES 16
|
||||
#define INLINE_PY_PARAM_SLOTS 64
|
||||
#define INLINE_PY_PARAM_STACK 64
|
||||
#define FLAT_LOOKBACK_LEVELS 4
|
||||
|
||||
/* Under parent, the search from sibling `asked` found this label, or none. */
|
||||
typedef struct {
|
||||
const void *parent;
|
||||
const void *asked;
|
||||
TSNode found;
|
||||
} CBMFlatLookback;
|
||||
|
||||
// ObjectScript type map: variable name → class name (for instance_method_call
|
||||
// resolution). Stack-allocated, per-method scope. Overflow is silent (no crash).
|
||||
@@ -129,6 +137,12 @@ typedef struct {
|
||||
uint8_t kind;
|
||||
} CBMPythonDirective;
|
||||
|
||||
// The lowest node a usage classifier has climbed from to the root undecided.
|
||||
typedef struct {
|
||||
TSNode node;
|
||||
uint32_t depth;
|
||||
} CBMOccurrenceClimb;
|
||||
|
||||
// WalkState tracks scope context during the unified cursor walk.
|
||||
// Replaces parent-chain walks for enclosing_func_qn, import context, etc.
|
||||
typedef struct {
|
||||
@@ -137,6 +151,10 @@ typedef struct {
|
||||
TSNode enclosing_class_node; // that class scope's node (null when not known)
|
||||
const TSTreeCursor *current_cursor; // unified walk cursor at the current node
|
||||
TSTreeCursor *occurrence_cursor; // reusable parent-preserving classifier cursor
|
||||
uint32_t walk_depth; // depth of the current node in the unified walk
|
||||
CBMOccurrenceClimb binding_climb; // settled by is_binding_occurrence
|
||||
CBMOccurrenceClimb label_climb; // settled by is_call_argument_label_walk
|
||||
CBMOccurrenceClimb write_climb; // settled by is_write_occurrence
|
||||
CBMInvocationKind invocation_kind; // exact active invocation/reference role
|
||||
TSNode callee_expr; // exact active callee expression, if any
|
||||
TSNode callee_leaf; // exact active terminal callee, if any
|
||||
@@ -163,6 +181,8 @@ typedef struct {
|
||||
uint32_t flat_anchor_start_byte;
|
||||
uint32_t flat_anchor_end_byte;
|
||||
const char *flat_function_qn;
|
||||
/* What the last flat label search found, one per ancestor level it looked at. */
|
||||
CBMFlatLookback flat_lookback[FLAT_LOOKBACK_LEVELS];
|
||||
|
||||
CBMLexicalBinding *lexical_bindings;
|
||||
CBMLexicalBinding inline_lexical_bindings[INLINE_LEXICAL_BINDINGS];
|
||||
|
||||
@@ -295,6 +295,21 @@ static TSTreeCursor *reset_occurrence_cursor(WalkState *state, TSNode node) {
|
||||
return state->occurrence_cursor;
|
||||
}
|
||||
|
||||
/* Grammars with few fields let a classifier climb to the root from every identifier,
|
||||
* so nesting d levels deep costs d per identifier. A climb that reaches the root
|
||||
* undecided settles every node on its way for all nodes below them: a level's answer
|
||||
* depends only on the path through it, and a node with any extent lies in one child of
|
||||
* its parent. The next climb stops where it meets that path: a node no deeper than the
|
||||
* settled one and covering it is the settled one or above it. */
|
||||
static bool occurrence_climb_settled(const CBMOccurrenceClimb *climb, TSNode current,
|
||||
uint32_t depth) {
|
||||
return climb && depth <= climb->depth && node_contains(current, climb->node);
|
||||
}
|
||||
|
||||
static CBMOccurrenceClimb *occurrence_climb_for(TSNode node, CBMOccurrenceClimb *climb) {
|
||||
return ts_node_end_byte(node) > ts_node_start_byte(node) ? climb : NULL;
|
||||
}
|
||||
|
||||
static bool occurrence_parent(TSTreeCursor *cursor, TSNode current, TSNode *parent,
|
||||
const char **field) {
|
||||
if (cursor) {
|
||||
@@ -1154,12 +1169,22 @@ static bool is_binding_occurrence(CBMExtractCtx *ctx, TSNode node, const CBMLang
|
||||
|
||||
TSNode current = node;
|
||||
TSTreeCursor *cursor = reset_occurrence_cursor(state, node);
|
||||
CBMOccurrenceClimb *climb = cursor ? occurrence_climb_for(node, &state->binding_climb) : NULL;
|
||||
uint32_t depth = cursor ? state->walk_depth : 0;
|
||||
TSNode first_parent = {0};
|
||||
while (!ts_node_is_null(current)) {
|
||||
TSNode parent;
|
||||
const char *field;
|
||||
if (occurrence_climb_settled(climb, current, depth)) {
|
||||
break;
|
||||
}
|
||||
if (!occurrence_parent(cursor, current, &parent, &field)) {
|
||||
break;
|
||||
}
|
||||
depth--;
|
||||
if (ts_node_is_null(first_parent)) {
|
||||
first_parent = parent;
|
||||
}
|
||||
if (is_value_field(field)) {
|
||||
return false;
|
||||
}
|
||||
@@ -1208,6 +1233,9 @@ static bool is_binding_occurrence(CBMExtractCtx *ctx, TSNode node, const CBMLang
|
||||
}
|
||||
current = parent;
|
||||
}
|
||||
if (climb && !ts_node_is_null(first_parent)) {
|
||||
*climb = (CBMOccurrenceClimb){first_parent, state->walk_depth - 1};
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1254,12 +1282,22 @@ static bool is_write_occurrence(CBMExtractCtx *ctx, TSNode node, const CBMLangSp
|
||||
const CBMOccurrenceSpec *occurrence = &occurrence_specs[ctx->language];
|
||||
TSNode current = node;
|
||||
TSTreeCursor *cursor = reset_occurrence_cursor(state, node);
|
||||
CBMOccurrenceClimb *climb = cursor ? occurrence_climb_for(node, &state->write_climb) : NULL;
|
||||
uint32_t depth = cursor ? state->walk_depth : 0;
|
||||
TSNode first_parent = {0};
|
||||
while (!ts_node_is_null(current)) {
|
||||
TSNode parent;
|
||||
const char *field;
|
||||
if (occurrence_climb_settled(climb, current, depth)) {
|
||||
break;
|
||||
}
|
||||
if (!occurrence_parent(cursor, current, &parent, &field)) {
|
||||
break;
|
||||
}
|
||||
depth--;
|
||||
if (ts_node_is_null(first_parent)) {
|
||||
first_parent = parent;
|
||||
}
|
||||
if (is_value_field(field)) {
|
||||
return false;
|
||||
}
|
||||
@@ -1291,6 +1329,9 @@ static bool is_write_occurrence(CBMExtractCtx *ctx, TSNode node, const CBMLangSp
|
||||
}
|
||||
current = parent;
|
||||
}
|
||||
if (climb && !ts_node_is_null(first_parent)) {
|
||||
*climb = (CBMOccurrenceClimb){first_parent, state->walk_depth - 1};
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1332,12 +1373,19 @@ static bool is_call_argument_label_walk(TSNode node, WalkState *state) {
|
||||
TSTreeCursor *cursor = reset_occurrence_cursor(state, node);
|
||||
if (!cursor)
|
||||
return is_call_argument_label(node);
|
||||
CBMOccurrenceClimb *climb = occurrence_climb_for(node, &state->label_climb);
|
||||
uint32_t depth = state->walk_depth;
|
||||
TSNode first_parent = {0};
|
||||
TSNode current = node;
|
||||
while (!ts_node_is_null(current)) {
|
||||
TSNode parent;
|
||||
const char *field;
|
||||
if (!occurrence_parent(cursor, current, &parent, &field))
|
||||
return false;
|
||||
if (occurrence_climb_settled(climb, current, depth) ||
|
||||
!occurrence_parent(cursor, current, &parent, &field))
|
||||
break;
|
||||
depth--;
|
||||
if (ts_node_is_null(first_parent))
|
||||
first_parent = parent;
|
||||
const char *parent_kind = ts_node_type(parent);
|
||||
if (is_labeled_argument_kind(parent_kind)) {
|
||||
return field && (strcmp(field, "name") == 0 || strcmp(field, "label") == 0 ||
|
||||
@@ -1347,6 +1395,8 @@ static bool is_call_argument_label_walk(TSNode node, WalkState *state) {
|
||||
return false;
|
||||
current = parent;
|
||||
}
|
||||
if (climb && !ts_node_is_null(first_parent))
|
||||
*climb = (CBMOccurrenceClimb){first_parent, state->walk_depth - 1};
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
+14
-1
@@ -874,7 +874,8 @@ static const char *func_kinds_ocaml[] = {"value_definition", "let_binding", NULL
|
||||
static const char *func_kinds_zig[] = {"function_declaration", "test_declaration", NULL};
|
||||
static const char *func_kinds_bash[] = {"function_definition", NULL};
|
||||
static const char *func_kinds_erlang[] = {"function_clause", NULL};
|
||||
static const char *func_kinds_csharp[] = {"method_declaration", "constructor_declaration", NULL};
|
||||
static const char *func_kinds_csharp[] = {"method_declaration", "constructor_declaration",
|
||||
"operator_declaration", NULL};
|
||||
static const char *func_kinds_matlab[] = {"function_definition", NULL};
|
||||
static const char *func_kinds_lean[] = {"def", "theorem", "instance", "abbrev", NULL};
|
||||
static const char *func_kinds_form[] = {"procedure_definition", NULL};
|
||||
@@ -1223,6 +1224,13 @@ char *cbm_func_name_node_text(CBMArena *a, TSNode name_node, const char *source,
|
||||
*paren = '\0';
|
||||
}
|
||||
}
|
||||
/* C# `operator +` is named by its token alone, an anonymous node; any other C# name
|
||||
* is a named node. Spelled like C++'s `operator+` and `operator bool`, here so the
|
||||
* def name and the call-scope QN cannot disagree. */
|
||||
if (text && lang == CBM_LANG_CSHARP && !ts_node_is_named(name_node)) {
|
||||
bool word = text[0] >= 'a' && text[0] <= 'z'; /* operator true, operator false */
|
||||
return cbm_arena_sprintf(a, "operator%s%s", word ? " " : "", text);
|
||||
}
|
||||
/* Nix quoted attrpath segment: `"kebab-case" = a: a;` and `services."my.svc" = …`
|
||||
* are ordinary names that merely need quoting in source. The node text carries
|
||||
* the delimiters, so without this the def is named `"kebab-case"` — quotes and
|
||||
@@ -1513,6 +1521,11 @@ static const char *func_node_name(CBMArena *a, TSNode func_node, const char *sou
|
||||
if (!ts_node_is_null(name_node)) {
|
||||
return cbm_node_text(a, name_node, source);
|
||||
}
|
||||
// C#: an overloaded operator has no name field, its token names it.
|
||||
if (lang == CBM_LANG_CSHARP && strcmp(ts_node_type(func_node), "operator_declaration") == 0) {
|
||||
TSNode op = ts_node_child_by_field_name(func_node, TS_FIELD("operator"));
|
||||
return ts_node_is_null(op) ? NULL : cbm_func_name_node_text(a, op, source, lang);
|
||||
}
|
||||
// Arrow functions: check parent variable_declarator
|
||||
if (strcmp(ts_node_type(func_node), "arrow_function") == 0) {
|
||||
TSNode parent = ts_node_parent(func_node);
|
||||
|
||||
@@ -365,14 +365,16 @@ static const char *cpp_branch_types[] = {"if_statement", "for_statement",
|
||||
"while_statement", "switch_statement", "case_statement",
|
||||
"try_statement", "catch_clause", NULL};
|
||||
static const char *cpp_var_types[] = {"declaration", NULL};
|
||||
/* No update_expression here, unlike C: a bare C++ name can be an implicit-this
|
||||
* member, so a local counter's `i++` would bind by name to an unrelated member. */
|
||||
static const char *cpp_assign_types[] = {"assignment_expression", NULL};
|
||||
static const char *cpp_throw_types[] = {"throw_statement", NULL};
|
||||
|
||||
// ==================== C# ====================
|
||||
static const char *cs_func_types[] = {"destructor_declaration", "local_function_statement",
|
||||
"function_pointer_type", "constructor_declaration",
|
||||
"anonymous_method_expression", "lambda_expression",
|
||||
"method_declaration", NULL};
|
||||
static const char *cs_func_types[] = {
|
||||
"destructor_declaration", "local_function_statement", "function_pointer_type",
|
||||
"constructor_declaration", "anonymous_method_expression", "lambda_expression",
|
||||
"method_declaration", "operator_declaration", NULL};
|
||||
static const char *cs_class_types[] = {"class_declaration",
|
||||
"struct_declaration",
|
||||
"enum_declaration",
|
||||
@@ -490,7 +492,7 @@ static const char *c_branch_types[] = {"if_statement",
|
||||
"case_statement",
|
||||
NULL};
|
||||
static const char *c_var_types[] = {"declaration", NULL};
|
||||
static const char *c_assign_types[] = {"assignment_expression", NULL};
|
||||
static const char *c_assign_types[] = {"assignment_expression", "update_expression", NULL};
|
||||
|
||||
// ==================== BASH ====================
|
||||
static const char *bash_func_types[] = {"function_definition", NULL};
|
||||
|
||||
@@ -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) ---
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* resolve_stats.c — deterministic work counters for growth tests (see the header).
|
||||
*/
|
||||
#include "resolve_stats.h"
|
||||
|
||||
#ifdef CBM_ENABLE_TEST_SEAMS
|
||||
#include <stddef.h>
|
||||
|
||||
static _Thread_local cbm_resolve_stats_t *current_stats;
|
||||
|
||||
void cbm_resolve_stats_reset(cbm_resolve_stats_t *stats) {
|
||||
if (stats) {
|
||||
for (unsigned i = 0; i < CBM_RESOLVE_STAT_COUNT; i++) {
|
||||
atomic_init(&stats->values[i], 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cbm_resolve_stats_t *cbm_resolve_stats_bind(cbm_resolve_stats_t *stats) {
|
||||
cbm_resolve_stats_t *previous = current_stats;
|
||||
current_stats = stats;
|
||||
return previous;
|
||||
}
|
||||
|
||||
void cbm_resolve_stats_add(cbm_resolve_stat_t event, uint64_t amount) {
|
||||
if (current_stats && (unsigned)event < CBM_RESOLVE_STAT_COUNT) {
|
||||
atomic_fetch_add_explicit(¤t_stats->values[event], amount, memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
cbm_resolve_stats_snapshot_t cbm_resolve_stats_snapshot(const cbm_resolve_stats_t *stats) {
|
||||
cbm_resolve_stats_snapshot_t result = {0};
|
||||
if (stats) {
|
||||
for (unsigned i = 0; i < CBM_RESOLVE_STAT_COUNT; i++) {
|
||||
result.values[i] = atomic_load_explicit(&stats->values[i], memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
const char *cbm_resolve_stat_name(cbm_resolve_stat_t event) {
|
||||
static const char *const names[] = {"lookup_probes", "syntax_child_steps"};
|
||||
_Static_assert(sizeof(names) / sizeof(names[0]) == CBM_RESOLVE_STAT_COUNT,
|
||||
"one name per counter");
|
||||
return (unsigned)event < CBM_RESOLVE_STAT_COUNT ? names[event] : "unknown";
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* resolve_stats.h — deterministic work counters for growth tests.
|
||||
*
|
||||
* Semantic work, never elapsed time or address-dependent hash placement, so a
|
||||
* test can double its input and assert the work about doubles (O9).
|
||||
* LOOKUP_PROBES counts keyed probes and comparisons; SYNTAX_CHILD_STEPS counts
|
||||
* the sibling steps taken to enumerate a node's children.
|
||||
*
|
||||
* Seam builds only (CBM_ENABLE_TEST_SEAMS), like lsp/lsp_work.h: nothing in a
|
||||
* product build reads these, so there cbm_resolve_stats_add compiles to nothing.
|
||||
*/
|
||||
#ifndef CBM_RESOLVE_STATS_H
|
||||
#define CBM_RESOLVE_STATS_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum {
|
||||
CBM_RESOLVE_STAT_LOOKUP_PROBES,
|
||||
CBM_RESOLVE_STAT_SYNTAX_CHILD_STEPS,
|
||||
CBM_RESOLVE_STAT_COUNT
|
||||
} cbm_resolve_stat_t;
|
||||
|
||||
#ifdef CBM_ENABLE_TEST_SEAMS
|
||||
#include <stdatomic.h>
|
||||
|
||||
typedef struct {
|
||||
_Atomic uint64_t values[CBM_RESOLVE_STAT_COUNT];
|
||||
} cbm_resolve_stats_t;
|
||||
typedef struct {
|
||||
uint64_t values[CBM_RESOLVE_STAT_COUNT];
|
||||
} cbm_resolve_stats_snapshot_t;
|
||||
|
||||
/* A sink is owned by its caller. Reset it before binding; snapshot it after
|
||||
* every thread bound to it has unbound. Bind returns the thread's previous
|
||||
* sink, which the caller must restore before leaving its scope. Work done on a
|
||||
* thread with no sink bound is not counted. No allocation is involved. */
|
||||
void cbm_resolve_stats_reset(cbm_resolve_stats_t *stats);
|
||||
cbm_resolve_stats_t *cbm_resolve_stats_bind(cbm_resolve_stats_t *stats);
|
||||
void cbm_resolve_stats_add(cbm_resolve_stat_t event, uint64_t amount);
|
||||
cbm_resolve_stats_snapshot_t cbm_resolve_stats_snapshot(const cbm_resolve_stats_t *stats);
|
||||
const char *cbm_resolve_stat_name(cbm_resolve_stat_t event);
|
||||
#else
|
||||
static inline void cbm_resolve_stats_add(cbm_resolve_stat_t event, uint64_t amount) {
|
||||
(void)event;
|
||||
(void)amount;
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* CBM_RESOLVE_STATS_H */
|
||||
+105
-15
@@ -6,7 +6,10 @@
|
||||
* `Runtime.getRuntime().exec`, a constructor as its class name.
|
||||
*/
|
||||
#include "spawn_patterns.h"
|
||||
#include "foundation/compat.h"
|
||||
#include "foundation/constants.h"
|
||||
#include "foundation/hash_table.h"
|
||||
#include "resolve_stats.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -415,6 +418,82 @@ static bool spawn_val_names(const char *val, const char *name) {
|
||||
return sep == '.' || sep == ':' || sep == '/' || sep == '\\';
|
||||
}
|
||||
|
||||
/* The rows of the file being walked, once a call has asked: every call asks about them. */
|
||||
typedef struct {
|
||||
const CBMImport *imports;
|
||||
int count;
|
||||
bool built;
|
||||
CBMHashTable *first_row; /* local name -> its first row with a path */
|
||||
CBMHashTable *modules; /* a spawn table's module -> spawn_module_yes or spawn_module_no */
|
||||
} spawn_file_t;
|
||||
static CBM_TLS spawn_file_t spawn_file;
|
||||
static const char spawn_module_yes[] = "y";
|
||||
static const char spawn_module_no[] = "n";
|
||||
|
||||
void cbm_spawn_file_end(void) {
|
||||
cbm_ht_free(spawn_file.first_row);
|
||||
cbm_ht_free(spawn_file.modules);
|
||||
spawn_file = (spawn_file_t){0};
|
||||
}
|
||||
|
||||
void cbm_spawn_file_begin(const CBMImport *imports, int count) {
|
||||
cbm_spawn_file_end();
|
||||
spawn_file.imports = imports;
|
||||
spawn_file.count = count;
|
||||
}
|
||||
|
||||
/* False: read the rows. These are not the file's, or memory ran out. */
|
||||
static bool spawn_file_ready(const CBMImport *imports, int count) {
|
||||
if (count <= 0 || spawn_file.imports != imports || spawn_file.count != count) {
|
||||
return false;
|
||||
}
|
||||
if (spawn_file.built) {
|
||||
return spawn_file.first_row != NULL;
|
||||
}
|
||||
spawn_file.built = true;
|
||||
CBMHashTable *rows = cbm_ht_create((uint32_t)count * 2U);
|
||||
CBMHashTable *modules = cbm_ht_create(16);
|
||||
bool ok = rows && modules;
|
||||
for (int i = 0; ok && i < count; i++) {
|
||||
cbm_resolve_stats_add(CBM_RESOLVE_STAT_LOOKUP_PROBES, 1);
|
||||
const char *key = imports[i].local_name;
|
||||
if (!key || !imports[i].module_path || cbm_ht_get(rows, key)) {
|
||||
continue;
|
||||
}
|
||||
cbm_ht_set(rows, key, (void *)&imports[i]);
|
||||
ok = cbm_ht_get(rows, key) == &imports[i];
|
||||
}
|
||||
if (!ok) {
|
||||
cbm_ht_free(rows);
|
||||
cbm_ht_free(modules);
|
||||
return false;
|
||||
}
|
||||
spawn_file.first_row = rows;
|
||||
spawn_file.modules = modules;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* The first row with a path that names the callee's first key_len bytes. */
|
||||
static const CBMImport *spawn_row_named(const char *callee, size_t key_len,
|
||||
const CBMImport *imports, int count) {
|
||||
char name[CBM_SZ_512];
|
||||
if (key_len < sizeof(name) && spawn_file_ready(imports, count)) {
|
||||
memcpy(name, callee, key_len);
|
||||
name[key_len] = '\0';
|
||||
cbm_resolve_stats_add(CBM_RESOLVE_STAT_LOOKUP_PROBES, 1);
|
||||
return cbm_ht_get(spawn_file.first_row, name);
|
||||
}
|
||||
for (int i = 0; i < count; i++) {
|
||||
cbm_resolve_stats_add(CBM_RESOLVE_STAT_LOOKUP_PROBES, 1);
|
||||
const char *key = imports[i].local_name;
|
||||
if (key && imports[i].module_path && strlen(key) == key_len &&
|
||||
strncmp(key, callee, key_len) == 0) {
|
||||
return &imports[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* The callee as its import makes it: `sp.run` -> subprocess.run (an alias),
|
||||
* `run` -> subprocess.run (a member import), `spawn` -> child_process.spawn
|
||||
* (a named import mapped to its module). Empty when no import applies. */
|
||||
@@ -423,27 +502,24 @@ static void spawn_expand(const char *callee, const CBMImport *imports, int count
|
||||
out[0] = '\0';
|
||||
size_t head = spawn_head_len(callee);
|
||||
size_t key_len = head ? head : strlen(callee);
|
||||
for (int i = 0; i < count; i++) {
|
||||
const char *key = imports[i].local_name;
|
||||
if (!key || !imports[i].module_path || strlen(key) != key_len ||
|
||||
strncmp(key, callee, key_len) != 0) {
|
||||
continue;
|
||||
}
|
||||
const char *val = spawn_module(imports[i].module_path);
|
||||
if (head) {
|
||||
snprintf(out, n, "%s%s", val, callee + head);
|
||||
} else if (spawn_val_names(val, callee)) {
|
||||
snprintf(out, n, "%s", val);
|
||||
} else {
|
||||
snprintf(out, n, "%s.%s", val, callee);
|
||||
}
|
||||
const CBMImport *row = spawn_row_named(callee, key_len, imports, count);
|
||||
if (!row) {
|
||||
return;
|
||||
}
|
||||
const char *val = spawn_module(row->module_path);
|
||||
if (head) {
|
||||
snprintf(out, n, "%s%s", val, callee + head);
|
||||
} else if (spawn_val_names(val, callee)) {
|
||||
snprintf(out, n, "%s", val);
|
||||
} else {
|
||||
snprintf(out, n, "%s.%s", val, callee);
|
||||
}
|
||||
}
|
||||
|
||||
static bool spawn_imports_module(const CBMImport *imports, int count, const char *module) {
|
||||
static bool spawn_rows_import_module(const CBMImport *imports, int count, const char *module) {
|
||||
size_t ml = strlen(module);
|
||||
for (int i = 0; i < count; i++) {
|
||||
cbm_resolve_stats_add(CBM_RESOLVE_STAT_LOOKUP_PROBES, 1);
|
||||
if (!imports[i].module_path) {
|
||||
continue;
|
||||
}
|
||||
@@ -456,6 +532,20 @@ static bool spawn_imports_module(const CBMImport *imports, int count, const char
|
||||
return false;
|
||||
}
|
||||
|
||||
/* A spawn table names a fixed few modules, so one file reads its rows that many times at most. */
|
||||
static bool spawn_imports_module(const CBMImport *imports, int count, const char *module) {
|
||||
if (!spawn_file_ready(imports, count)) {
|
||||
return spawn_rows_import_module(imports, count, module);
|
||||
}
|
||||
const void *known = cbm_ht_get(spawn_file.modules, module);
|
||||
if (known) {
|
||||
return known == spawn_module_yes;
|
||||
}
|
||||
bool found = spawn_rows_import_module(imports, count, module);
|
||||
cbm_ht_set(spawn_file.modules, module, (void *)(found ? spawn_module_yes : spawn_module_no));
|
||||
return found;
|
||||
}
|
||||
|
||||
/* Groovy `"git status".execute`: a quoted receiver, then the api suffix. */
|
||||
static bool spawn_string_receiver(const char *callee, const char *suffix) {
|
||||
size_t cl = strlen(callee);
|
||||
|
||||
@@ -59,6 +59,11 @@ typedef struct {
|
||||
const cbm_spawn_api_t *cbm_spawn_match(CBMLanguage lang, const char *callee,
|
||||
const CBMImport *imports, int count);
|
||||
|
||||
/* Around one file's loop over its calls, on one thread: a match on exactly these rows asks them
|
||||
* by name, the same answer as reading them all. Any other rows are read as usual. */
|
||||
void cbm_spawn_file_begin(const CBMImport *imports, int count);
|
||||
void cbm_spawn_file_end(void);
|
||||
|
||||
/* The program a matched spawn starts: the first literal in its program
|
||||
* argument, that literal's first word, without directories
|
||||
* ("/usr/bin/git status" -> "git"). CBM_SPAWN_DYNAMIC when the program is
|
||||
|
||||
@@ -73,7 +73,7 @@ src/pipeline/pass_pkgmap.c 80
|
||||
src/pipeline/pass_semantic.c 3
|
||||
src/pipeline/pass_tests.c 3
|
||||
src/pipeline/pass_usages.c 4
|
||||
src/pipeline/path_alias.c 40
|
||||
src/pipeline/path_alias.c 1
|
||||
src/pipeline/pipeline.c 129
|
||||
src/pipeline/pipeline_delta.c 19
|
||||
src/pipeline/pipeline_incremental.c 130
|
||||
|
||||
+11
-3
@@ -7518,6 +7518,14 @@ int cbm_cli_remove_windows_user_path_for_test(const char *bin_dir, bool dry_run)
|
||||
*/
|
||||
#ifdef CBM_CLI_ENABLE_TEST_API
|
||||
|
||||
/* The most an archive may decompress to. A test lowers it to pin the refusal
|
||||
* without building an archive of hundreds of megabytes. */
|
||||
static size_t g_cli_decompress_max = DECOMPRESS_MAX_BYTES;
|
||||
|
||||
void cbm_cli_set_decompress_max_for_test(size_t max_bytes) {
|
||||
g_cli_decompress_max = max_bytes ? max_bytes : DECOMPRESS_MAX_BYTES;
|
||||
}
|
||||
|
||||
/* Decompress gzip data into a malloc'd buffer. Returns NULL on failure.
|
||||
* *out_total receives the decompressed size. Caller must free the result. */
|
||||
static unsigned char *gzip_decompress(const unsigned char *data, int data_len, size_t *out_total) {
|
||||
@@ -7535,8 +7543,8 @@ static unsigned char *gzip_decompress(const unsigned char *data, int data_len, s
|
||||
if (buf_cap < CLI_BUF_4K) {
|
||||
buf_cap = CLI_BUF_4K;
|
||||
}
|
||||
if (buf_cap > DECOMPRESS_MAX_BYTES) {
|
||||
buf_cap = DECOMPRESS_MAX_BYTES;
|
||||
if (buf_cap > g_cli_decompress_max) {
|
||||
buf_cap = g_cli_decompress_max;
|
||||
}
|
||||
unsigned char *decompressed = malloc(buf_cap);
|
||||
if (!decompressed) {
|
||||
@@ -7549,7 +7557,7 @@ static unsigned char *gzip_decompress(const unsigned char *data, int data_len, s
|
||||
do {
|
||||
if (total >= buf_cap) {
|
||||
size_t new_cap = buf_cap * GROWTH_FACTOR;
|
||||
if (new_cap > DECOMPRESS_MAX_BYTES) {
|
||||
if (new_cap > g_cli_decompress_max) {
|
||||
free(decompressed);
|
||||
inflateEnd(&strm);
|
||||
return NULL;
|
||||
|
||||
@@ -480,6 +480,10 @@ unsigned char *cbm_extract_binary_from_targz(const unsigned char *data, int data
|
||||
* Returns NULL on error. Caller must free. */
|
||||
unsigned char *cbm_extract_binary_from_zip(const unsigned char *data, int data_len, int *out_len);
|
||||
|
||||
/* Lower the limit a tar.gz archive may decompress to, so a test can pin the
|
||||
* refusal with a small archive. 0 restores the 500 MiB limit. */
|
||||
void cbm_cli_set_decompress_max_for_test(size_t max_bytes);
|
||||
|
||||
#endif /* CBM_CLI_ENABLE_TEST_API */
|
||||
|
||||
/* ── Index management ─────────────────────────────────────────── */
|
||||
|
||||
@@ -1630,6 +1630,7 @@ int cbm_pipeline_pass_calls(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *file
|
||||
|
||||
/* Resolve each call */
|
||||
cbm_external_import_index_t ext_imports = {0};
|
||||
cbm_spawn_file_begin(result->imports.items, result->imports.count);
|
||||
for (int c = 0; c < result->calls.count; c++) {
|
||||
CBMCall *call = &result->calls.items[c];
|
||||
if (!call->callee_name) {
|
||||
@@ -1646,6 +1647,7 @@ int cbm_pipeline_pass_calls(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *file
|
||||
unresolved++;
|
||||
}
|
||||
}
|
||||
cbm_spawn_file_end();
|
||||
cbm_pipeline_external_import_index_free(&ext_imports);
|
||||
|
||||
cbm_pipeline_record_unresolved_calls(ctx->pipeline, rel, result, evidence);
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include "pipeline/lsp_surface.h"
|
||||
#include "foundation/mem_core.h"
|
||||
#include "result_spill.h"
|
||||
#include "resolve_stats.h"
|
||||
#include "pipeline/pipeline_internal.h"
|
||||
#include "pipeline/lsp_resolve.h"
|
||||
#include "callable_sig.h"
|
||||
@@ -759,23 +760,36 @@ static int pxc_build_lsp_def(CBMArena *arena, const CBMDefinition *src, const ch
|
||||
* "Class" for a C struct or union. */
|
||||
static void pxc_fold_struct_fields(CBMArena *arena, const CBMFileResult *result, CBMLSPDef *defs,
|
||||
int start, int end, const char *type_label) {
|
||||
if (!arena || !result || !defs || start >= end) {
|
||||
if (!arena || !result || !defs || start >= end || result->defs.count <= 0) {
|
||||
return;
|
||||
}
|
||||
for (int si = start; si < end; si++) {
|
||||
/* A generated file holds hundreds of types and thousands of fields: each type's
|
||||
* fields are one chain, in definition order, instead of a scan of the file per type.
|
||||
* next_field[di] is the 1-based index of the field after di of the same owner. */
|
||||
int *next_field =
|
||||
cbm_alloc(CBM_MEM_CLASS_EXTRACT, (size_t)result->defs.count * sizeof(*next_field));
|
||||
CBMHashTable *fields_of = cbm_ht_create_in(CBM_MEM_CLASS_EXTRACT, 32);
|
||||
bool joined = next_field && fields_of;
|
||||
for (int di = result->defs.count - 1; joined && di >= 0; di--) {
|
||||
const CBMDefinition *fd = &result->defs.items[di];
|
||||
if (!fd->label || !fd->parent_class || !fd->name || !fd->name[0] || !fd->return_type ||
|
||||
!fd->return_type[0] || strcmp(fd->label, "Field") != 0) {
|
||||
continue;
|
||||
}
|
||||
next_field[di] =
|
||||
(int)(intptr_t)cbm_ht_set(fields_of, fd->parent_class, (void *)(intptr_t)(di + 1));
|
||||
}
|
||||
for (int si = start; joined && si < end; si++) {
|
||||
CBMLSPDef *dst = &defs[si];
|
||||
if (!dst->label || strcmp(dst->label, type_label) != 0 || !dst->qualified_name) {
|
||||
continue;
|
||||
}
|
||||
int count = 0;
|
||||
size_t total = 0; /* "name:type" bytes; separators and NUL added below */
|
||||
for (int di = 0; di < result->defs.count; di++) {
|
||||
const CBMDefinition *fd = &result->defs.items[di];
|
||||
if (!fd->label || !fd->parent_class || !fd->name || !fd->name[0] || !fd->return_type ||
|
||||
!fd->return_type[0] || strcmp(fd->label, "Field") != 0 ||
|
||||
strcmp(fd->parent_class, dst->qualified_name) != 0) {
|
||||
continue;
|
||||
}
|
||||
int first_field = (int)(intptr_t)cbm_ht_get(fields_of, dst->qualified_name);
|
||||
for (int link = first_field; link; link = next_field[link - 1]) {
|
||||
cbm_resolve_stats_add(CBM_RESOLVE_STAT_LOOKUP_PROBES, 1);
|
||||
const CBMDefinition *fd = &result->defs.items[link - 1];
|
||||
total += strlen(fd->name) + 1 + strlen(fd->return_type);
|
||||
count++;
|
||||
}
|
||||
@@ -790,13 +804,8 @@ static void pxc_fold_struct_fields(CBMArena *arena, const CBMFileResult *result,
|
||||
}
|
||||
char *p = buf;
|
||||
int written = 0;
|
||||
for (int di = 0; di < result->defs.count; di++) {
|
||||
const CBMDefinition *fd = &result->defs.items[di];
|
||||
if (!fd->label || !fd->parent_class || !fd->name || !fd->name[0] || !fd->return_type ||
|
||||
!fd->return_type[0] || strcmp(fd->label, "Field") != 0 ||
|
||||
strcmp(fd->parent_class, dst->qualified_name) != 0) {
|
||||
continue;
|
||||
}
|
||||
for (int link = first_field; link; link = next_field[link - 1]) {
|
||||
const CBMDefinition *fd = &result->defs.items[link - 1];
|
||||
size_t n = strlen(fd->name);
|
||||
memcpy(p, fd->name, n);
|
||||
p += n;
|
||||
@@ -812,6 +821,8 @@ static void pxc_fold_struct_fields(CBMArena *arena, const CBMFileResult *result,
|
||||
*p = '\0';
|
||||
dst->field_defs = buf;
|
||||
}
|
||||
cbm_ht_free(fields_of);
|
||||
cbm_free(CBM_MEM_CLASS_EXTRACT, next_field);
|
||||
}
|
||||
|
||||
/* Python annotation text -> the one type name it declares, or NULL. Strips a
|
||||
|
||||
@@ -3352,6 +3352,7 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
|
||||
cbm_pipeline_lsp_field_index_t field_index = {0};
|
||||
bool field_index_built = false;
|
||||
bool field_index_ready = false;
|
||||
cbm_spawn_file_begin(result->imports.items, result->imports.count);
|
||||
for (int c = 0; c < result->calls.count; c++) {
|
||||
const CBMCall *call = &result->calls.items[c];
|
||||
CBMCallEvidence fallback = {0};
|
||||
@@ -3831,6 +3832,7 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
|
||||
cbm_ht_foreach(lsp_legacy_idx, lsp_idx_free_key, NULL);
|
||||
cbm_ht_free(lsp_legacy_idx);
|
||||
}
|
||||
cbm_spawn_file_end();
|
||||
cbm_pipeline_external_import_index_free(&ext_imports);
|
||||
cbm_pipeline_lsp_field_index_free(&field_index);
|
||||
}
|
||||
|
||||
+57
-41
@@ -26,6 +26,7 @@
|
||||
#include "foundation/compat_fs.h"
|
||||
#include "foundation/constants.h"
|
||||
#include "foundation/log.h"
|
||||
#include "foundation/mem_core.h"
|
||||
#include "foundation/platform.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
@@ -45,8 +46,22 @@ enum {
|
||||
CBM_PATH_ALIAS_MAX_DEPTH = 32, /* directory recursion depth */
|
||||
};
|
||||
|
||||
/* Everything a collection owns comes from the memory core in this class and
|
||||
* goes back through cbm_path_alias_collection_free. */
|
||||
#define ALIAS_MEM CBM_MEM_CLASS_OTHER
|
||||
|
||||
/* ── Helpers ───────────────────────────────────────────────────── */
|
||||
|
||||
/* Copy the first length bytes of text, which holds no NUL before them. */
|
||||
static char *alias_strndup(const char *text, size_t length) {
|
||||
char *copy = cbm_alloc(ALIAS_MEM, length + 1);
|
||||
if (copy) {
|
||||
memcpy(copy, text, length);
|
||||
copy[length] = '\0';
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
|
||||
/* Strip .ts/.tsx/.js/.jsx in place. Returns its argument. */
|
||||
static char *strip_resolved_ext(char *path) {
|
||||
if (!path) {
|
||||
@@ -82,7 +97,7 @@ static char *resolve_target_relative(const char *dir_prefix, const char *target)
|
||||
}
|
||||
size_t dp_len = (dir_prefix && dir_prefix[0] != '\0') ? strlen(dir_prefix) : 0;
|
||||
size_t t_len = strlen(target);
|
||||
char *buf = malloc(dp_len + t_len + 2);
|
||||
char *buf = cbm_alloc(ALIAS_MEM, dp_len + t_len + 2);
|
||||
if (!buf) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -183,7 +198,7 @@ static cbm_path_alias_map_t *load_tsconfig_file(const char *abs_path, const char
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
char *buf = malloc((size_t)len + 1);
|
||||
char *buf = cbm_alloc(ALIAS_MEM, (size_t)len + 1);
|
||||
if (!buf) {
|
||||
fclose(f);
|
||||
return NULL;
|
||||
@@ -194,7 +209,7 @@ static cbm_path_alias_map_t *load_tsconfig_file(const char *abs_path, const char
|
||||
|
||||
yyjson_read_flag flg = YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS;
|
||||
yyjson_doc *doc = yyjson_read(buf, nread, flg);
|
||||
free(buf);
|
||||
cbm_free(ALIAS_MEM, buf);
|
||||
if (!doc) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -212,7 +227,7 @@ static cbm_path_alias_map_t *load_tsconfig_file(const char *abs_path, const char
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cbm_path_alias_map_t *map = calloc(1, sizeof(*map));
|
||||
cbm_path_alias_map_t *map = cbm_calloc(ALIAS_MEM, sizeof(*map));
|
||||
if (!map) {
|
||||
yyjson_doc_free(doc);
|
||||
return NULL;
|
||||
@@ -222,7 +237,7 @@ static cbm_path_alias_map_t *load_tsconfig_file(const char *abs_path, const char
|
||||
map->base_url = resolve_target_relative(dir_prefix, base_url_str);
|
||||
} else if (base_url_str && strcmp(base_url_str, ".") == 0 && dir_prefix &&
|
||||
dir_prefix[0] != '\0') {
|
||||
map->base_url = strdup(dir_prefix);
|
||||
map->base_url = cbm_mem_strdup(ALIAS_MEM, dir_prefix);
|
||||
}
|
||||
|
||||
if (paths_obj && yyjson_is_obj(paths_obj)) {
|
||||
@@ -230,10 +245,10 @@ static cbm_path_alias_map_t *load_tsconfig_file(const char *abs_path, const char
|
||||
bool capped = obj_size > CBM_PATH_ALIAS_MAX_ENTRIES;
|
||||
int capacity = (int)(capped ? (size_t)CBM_PATH_ALIAS_MAX_ENTRIES : obj_size);
|
||||
if (capacity > 0) {
|
||||
map->entries = calloc((size_t)capacity, sizeof(cbm_path_alias_t));
|
||||
map->entries = cbm_calloc(ALIAS_MEM, (size_t)capacity * sizeof(cbm_path_alias_t));
|
||||
if (!map->entries) {
|
||||
free(map->base_url);
|
||||
free(map);
|
||||
cbm_free(ALIAS_MEM, map->base_url);
|
||||
cbm_free(ALIAS_MEM, map);
|
||||
yyjson_doc_free(doc);
|
||||
return NULL;
|
||||
}
|
||||
@@ -254,22 +269,22 @@ static cbm_path_alias_map_t *load_tsconfig_file(const char *abs_path, const char
|
||||
if (star) {
|
||||
entry->has_wildcard = true;
|
||||
entry->alias_prefix =
|
||||
cbm_strndup(alias_pattern, (size_t)(star - alias_pattern));
|
||||
entry->alias_suffix = strdup(star + 1);
|
||||
alias_strndup(alias_pattern, (size_t)(star - alias_pattern));
|
||||
entry->alias_suffix = cbm_mem_strdup(ALIAS_MEM, star + 1);
|
||||
} else {
|
||||
entry->has_wildcard = false;
|
||||
entry->alias_prefix = strdup(alias_pattern);
|
||||
entry->alias_suffix = strdup("");
|
||||
entry->alias_prefix = cbm_mem_strdup(ALIAS_MEM, alias_pattern);
|
||||
entry->alias_suffix = cbm_mem_strdup(ALIAS_MEM, "");
|
||||
}
|
||||
const char *tstar = strchr(target_pattern, '*');
|
||||
if (tstar) {
|
||||
char *pre = cbm_strndup(target_pattern, (size_t)(tstar - target_pattern));
|
||||
char *pre = alias_strndup(target_pattern, (size_t)(tstar - target_pattern));
|
||||
entry->target_prefix = resolve_target_relative(dir_prefix, pre);
|
||||
free(pre);
|
||||
entry->target_suffix = strdup(tstar + 1);
|
||||
cbm_free(ALIAS_MEM, pre);
|
||||
entry->target_suffix = cbm_mem_strdup(ALIAS_MEM, tstar + 1);
|
||||
} else {
|
||||
entry->target_prefix = resolve_target_relative(dir_prefix, target_pattern);
|
||||
entry->target_suffix = strdup("");
|
||||
entry->target_suffix = cbm_mem_strdup(ALIAS_MEM, "");
|
||||
}
|
||||
map->count++;
|
||||
}
|
||||
@@ -294,14 +309,14 @@ static void free_alias_map(cbm_path_alias_map_t *map) {
|
||||
return;
|
||||
}
|
||||
for (int j = 0; j < map->count; j++) {
|
||||
free(map->entries[j].alias_prefix);
|
||||
free(map->entries[j].alias_suffix);
|
||||
free(map->entries[j].target_prefix);
|
||||
free(map->entries[j].target_suffix);
|
||||
cbm_free(ALIAS_MEM, map->entries[j].alias_prefix);
|
||||
cbm_free(ALIAS_MEM, map->entries[j].alias_suffix);
|
||||
cbm_free(ALIAS_MEM, map->entries[j].target_prefix);
|
||||
cbm_free(ALIAS_MEM, map->entries[j].target_suffix);
|
||||
}
|
||||
free(map->entries);
|
||||
free(map->base_url);
|
||||
free(map);
|
||||
cbm_free(ALIAS_MEM, map->entries);
|
||||
cbm_free(ALIAS_MEM, map->base_url);
|
||||
cbm_free(ALIAS_MEM, map);
|
||||
}
|
||||
|
||||
void cbm_path_alias_collection_free(cbm_path_alias_collection_t *coll) {
|
||||
@@ -309,12 +324,12 @@ void cbm_path_alias_collection_free(cbm_path_alias_collection_t *coll) {
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < coll->count; i++) {
|
||||
free(coll->scopes[i].dir_prefix);
|
||||
free(coll->scopes[i].source_rel_path);
|
||||
cbm_free(ALIAS_MEM, coll->scopes[i].dir_prefix);
|
||||
cbm_free(ALIAS_MEM, coll->scopes[i].source_rel_path);
|
||||
free_alias_map(coll->scopes[i].map);
|
||||
}
|
||||
free(coll->scopes);
|
||||
free(coll);
|
||||
cbm_free(ALIAS_MEM, coll->scopes);
|
||||
cbm_free(ALIAS_MEM, coll);
|
||||
}
|
||||
|
||||
bool cbm_path_alias_resolve_into(const cbm_path_alias_map_t *map, const char *module_path,
|
||||
@@ -367,6 +382,7 @@ bool cbm_path_alias_resolve_into(const cbm_path_alias_map_t *map, const char *mo
|
||||
return true;
|
||||
}
|
||||
|
||||
/* The result stays on the C allocator: its callers release it with free(). */
|
||||
char *cbm_path_alias_resolve(const cbm_path_alias_map_t *map, const char *module_path) {
|
||||
char buf[CBM_PATH_ALIAS_RESULT_MAX];
|
||||
return cbm_path_alias_resolve_into(map, module_path, buf, sizeof(buf)) ? strdup(buf) : NULL;
|
||||
@@ -450,7 +466,7 @@ cbm_path_alias_collection_t *cbm_load_path_aliases_excluded(const char *repo_pat
|
||||
if (!repo_path) {
|
||||
return NULL;
|
||||
}
|
||||
alias_config_hit_t *hits = calloc(CBM_PATH_ALIAS_MAX_FILES, sizeof(*hits));
|
||||
alias_config_hit_t *hits = cbm_calloc(ALIAS_MEM, CBM_PATH_ALIAS_MAX_FILES * sizeof(*hits));
|
||||
if (!hits) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -461,19 +477,19 @@ cbm_path_alias_collection_t *cbm_load_path_aliases_excluded(const char *repo_pat
|
||||
cbm_log_warn("path_alias.files.cap_hit", "repo", repo_path, "kept", "256_of_more");
|
||||
}
|
||||
if (count == 0) {
|
||||
free(hits);
|
||||
cbm_free(ALIAS_MEM, hits);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cbm_path_alias_collection_t *coll = calloc(1, sizeof(*coll));
|
||||
cbm_path_alias_collection_t *coll = cbm_calloc(ALIAS_MEM, sizeof(*coll));
|
||||
if (!coll) {
|
||||
free(hits);
|
||||
cbm_free(ALIAS_MEM, hits);
|
||||
return NULL;
|
||||
}
|
||||
coll->scopes = calloc((size_t)count, sizeof(cbm_path_alias_scope_t));
|
||||
coll->scopes = cbm_calloc(ALIAS_MEM, (size_t)count * sizeof(cbm_path_alias_scope_t));
|
||||
if (!coll->scopes) {
|
||||
free(coll);
|
||||
free(hits);
|
||||
cbm_free(ALIAS_MEM, coll);
|
||||
cbm_free(ALIAS_MEM, hits);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -490,11 +506,11 @@ cbm_path_alias_collection_t *cbm_load_path_aliases_excluded(const char *repo_pat
|
||||
} else {
|
||||
snprintf(selected_rel, sizeof(selected_rel), "%s", base);
|
||||
}
|
||||
char *dir_prefix = strdup(hits[i].rel);
|
||||
char *source_rel_path = strdup(selected_rel);
|
||||
char *dir_prefix = cbm_mem_strdup(ALIAS_MEM, hits[i].rel);
|
||||
char *source_rel_path = cbm_mem_strdup(ALIAS_MEM, selected_rel);
|
||||
if (!dir_prefix || !source_rel_path) {
|
||||
free(dir_prefix);
|
||||
free(source_rel_path);
|
||||
cbm_free(ALIAS_MEM, dir_prefix);
|
||||
cbm_free(ALIAS_MEM, source_rel_path);
|
||||
free_alias_map(map);
|
||||
continue;
|
||||
}
|
||||
@@ -503,11 +519,11 @@ cbm_path_alias_collection_t *cbm_load_path_aliases_excluded(const char *repo_pat
|
||||
coll->scopes[coll->count].map = map;
|
||||
coll->count++;
|
||||
}
|
||||
free(hits);
|
||||
cbm_free(ALIAS_MEM, hits);
|
||||
|
||||
if (coll->count == 0) {
|
||||
free(coll->scopes);
|
||||
free(coll);
|
||||
cbm_free(ALIAS_MEM, coll->scopes);
|
||||
cbm_free(ALIAS_MEM, coll);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
@@ -7121,6 +7121,36 @@ TEST(cli_extract_binary_from_targz_invalid_data) {
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* The decompression limit keeps a hostile archive from inflating without end.
|
||||
* The same archive is refused over a lowered limit and accepted under the real
|
||||
* one, so the limit is the only reason for the refusal. */
|
||||
TEST(cli_update_refuses_an_archive_over_the_decompression_limit) {
|
||||
enum { PAYLOAD = 1 << 20, LIMIT = 1 << 16 };
|
||||
unsigned char *payload = calloc(1, PAYLOAD);
|
||||
ASSERT_NOT_NULL(payload);
|
||||
int archive_len = 0;
|
||||
unsigned char *archive =
|
||||
create_test_targz("codebase-memory-mcp", payload, PAYLOAD, &archive_len);
|
||||
free(payload);
|
||||
ASSERT_NOT_NULL(archive);
|
||||
|
||||
cbm_cli_set_decompress_max_for_test(LIMIT);
|
||||
int over_len = 0;
|
||||
unsigned char *over = cbm_extract_binary_from_targz(archive, archive_len, &over_len);
|
||||
cbm_cli_set_decompress_max_for_test(0);
|
||||
int under_len = 0;
|
||||
unsigned char *under = cbm_extract_binary_from_targz(archive, archive_len, &under_len);
|
||||
free(archive);
|
||||
|
||||
bool refused = over == NULL;
|
||||
free(over);
|
||||
bool accepted = under != NULL && under_len == PAYLOAD;
|
||||
free(under);
|
||||
ASSERT_TRUE(refused);
|
||||
ASSERT_TRUE(accepted);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* ═══════════════════════════════════════════════════════════════════
|
||||
* Zip extraction tests
|
||||
* ═══════════════════════════════════════════════════════════════════ */
|
||||
@@ -21449,6 +21479,7 @@ SUITE(cli) {
|
||||
RUN_TEST(cli_extract_binary_from_targz);
|
||||
RUN_TEST(cli_extract_binary_from_targz_not_found);
|
||||
RUN_TEST(cli_extract_binary_from_targz_invalid_data);
|
||||
RUN_TEST(cli_update_refuses_an_archive_over_the_decompression_limit);
|
||||
RUN_TEST(cli_extract_binary_from_zip);
|
||||
RUN_TEST(cli_extract_binary_from_zip_not_found);
|
||||
RUN_TEST(cli_extract_binary_from_zip_path_traversal);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -624,6 +624,62 @@ TEST(handles_gin_group_prefix_issue686) {
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* #686: which statement binds a group receiver. `var` lists and blocks, tuples, a name
|
||||
* declared with no value (no prefix), a shadowing inner block, rebinding, an enclosing
|
||||
* block reached from an inner one, a chain of groups, and a function literal (a boundary:
|
||||
* nothing outside it binds). */
|
||||
static const EtFile et_go_group_bindings[] = {{"main.go",
|
||||
"package main\n\n"
|
||||
"import \"github.com/gin-gonic/gin\"\n\n"
|
||||
"func h(c *gin.Context) {}\n\n"
|
||||
"func main() {\n"
|
||||
" r := gin.Default()\n"
|
||||
" var a, b = r.Group(\"/a\"), r.Group(\"/b\")\n"
|
||||
" a.GET(\"/x\", h)\n"
|
||||
" b.GET(\"/y\", h)\n"
|
||||
" var (\n"
|
||||
" c1 = r.Group(\"/c\")\n"
|
||||
" d1 *gin.RouterGroup\n"
|
||||
" )\n"
|
||||
" c1.GET(\"/z\", h)\n"
|
||||
" d1.GET(\"/w\", h)\n"
|
||||
" e, f := r.Group(\"/e\"), r.Group(\"/f\")\n"
|
||||
" f.GET(\"/q\", h)\n"
|
||||
" e.GET(\"/p\", h)\n"
|
||||
" g := r.Group(\"/g1\")\n"
|
||||
" {\n"
|
||||
" g := r.Group(\"/g2\")\n"
|
||||
" g.GET(\"/in\", h)\n"
|
||||
" }\n"
|
||||
" g.GET(\"/out\", h)\n"
|
||||
" g = r.Group(\"/g3\")\n"
|
||||
" if true {\n"
|
||||
" g.GET(\"/if\", h)\n"
|
||||
" }\n"
|
||||
" m := r.Group(\"/m\")\n"
|
||||
" n := m.Group(\"/n\")\n"
|
||||
" n.GET(\"/deep\", h)\n"
|
||||
" go func() {\n"
|
||||
" g.GET(\"/lit\", h)\n"
|
||||
" }()\n"
|
||||
"}\n"}};
|
||||
|
||||
static const char *et_go_group_bindings_routes[] = {"/a/x", "/b/y", "/c/z", "/w",
|
||||
"/f/q", "/e/p", "/g2/in", "/g1/out",
|
||||
"/g3/if", "/m/n/deep", "/lit", NULL};
|
||||
|
||||
TEST(handles_go_group_binding_forms_sequential_issue686) {
|
||||
ASSERT_TRUE(
|
||||
et_routes_exact_mode(et_go_group_bindings, 1, et_go_group_bindings_routes, false, NULL));
|
||||
PASS();
|
||||
}
|
||||
|
||||
TEST(handles_go_group_binding_forms_parallel_issue686) {
|
||||
ASSERT_TRUE(
|
||||
et_routes_exact_mode(et_go_group_bindings, 1, et_go_group_bindings_routes, true, NULL));
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* Spring (Java) — class-level @RequestMapping must prefix method mappings.
|
||||
* Reproduce-first: a HANDLES count alone can pass with partial routes
|
||||
* ("/orders"), but callers/search_graph need the actual endpoint names
|
||||
@@ -3003,6 +3059,8 @@ SUITE(edge_types_probe) {
|
||||
RUN_TEST(handles_fiber_group_prefix_sequential_issue686);
|
||||
RUN_TEST(handles_fiber_group_prefix_parallel_issue686);
|
||||
RUN_TEST(handles_gin_group_prefix_issue686);
|
||||
RUN_TEST(handles_go_group_binding_forms_sequential_issue686);
|
||||
RUN_TEST(handles_go_group_binding_forms_parallel_issue686);
|
||||
RUN_TEST(handles_spring_java);
|
||||
RUN_TEST(data_flows_test_files_pair_within_their_file_sequential);
|
||||
RUN_TEST(data_flows_test_files_pair_within_their_file_parallel);
|
||||
|
||||
@@ -174,6 +174,62 @@ static int mkc_edge(const MKC_File *files, int nfiles, const char *edge_type, in
|
||||
return got >= floor;
|
||||
}
|
||||
|
||||
/* Nodes carrying `label` whose qualified name ends with `qn_suffix`. */
|
||||
static int mkc_label_nodes_by_qn_suffix(cbm_store_t *store, const char *project, const char *label,
|
||||
const char *qn_suffix) {
|
||||
cbm_node_t *nodes = NULL;
|
||||
int count = 0;
|
||||
if (!store || cbm_store_find_nodes_by_qn_suffix(store, project, qn_suffix, &nodes, &count) !=
|
||||
CBM_STORE_OK) {
|
||||
return -1;
|
||||
}
|
||||
int matches = 0;
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (nodes[i].label && strcmp(nodes[i].label, label) == 0) {
|
||||
matches++;
|
||||
}
|
||||
}
|
||||
cbm_store_free_nodes(nodes, count);
|
||||
return matches;
|
||||
}
|
||||
|
||||
/* Edges of `edge_type` from the one node ending in `source_suffix` to nodes named
|
||||
* `target_name`. -1 when the suffix names no node or more than one, so a missing
|
||||
* source node is told apart from a missing edge. A per-type total cannot see
|
||||
* WHICH scope a call was attributed to. The target is matched by name so a
|
||||
* constructor call counts whether it binds the type or its constructor. */
|
||||
static int mkc_edges_from_suffix_to_name(cbm_store_t *store, const char *project,
|
||||
const char *edge_type, const char *source_suffix,
|
||||
const char *target_name) {
|
||||
cbm_node_t *sources = NULL;
|
||||
int source_count = 0;
|
||||
if (!store ||
|
||||
cbm_store_find_nodes_by_qn_suffix(store, project, source_suffix, &sources, &source_count) !=
|
||||
CBM_STORE_OK ||
|
||||
source_count != 1) {
|
||||
cbm_store_free_nodes(sources, source_count);
|
||||
return -1;
|
||||
}
|
||||
cbm_edge_t *edges = NULL;
|
||||
int edge_count = 0;
|
||||
int matches = -1;
|
||||
if (cbm_store_find_edges_by_source_type(store, sources[0].id, edge_type, &edges, &edge_count) ==
|
||||
CBM_STORE_OK) {
|
||||
matches = 0;
|
||||
for (int i = 0; i < edge_count; i++) {
|
||||
cbm_node_t target = {0};
|
||||
if (cbm_store_find_node_by_id(store, edges[i].target_id, &target) == CBM_STORE_OK &&
|
||||
target.name && strcmp(target.name, target_name) == 0) {
|
||||
matches++;
|
||||
}
|
||||
cbm_node_free_fields(&target);
|
||||
}
|
||||
}
|
||||
cbm_store_free_edges(edges, edge_count);
|
||||
cbm_store_free_nodes(sources, source_count);
|
||||
return matches;
|
||||
}
|
||||
|
||||
/* ══════════════════════════════════════════════════════════════════════════
|
||||
* ═══ CLASS C1: CONSTRUCTOR / INSTANTIATION ═══════════════════════════════
|
||||
*
|
||||
@@ -475,9 +531,13 @@ TEST(mkc_c2_kotlin_operator_plus) {
|
||||
}
|
||||
|
||||
/* C2-G: C# — operator + overload.
|
||||
* red=bug: C# `a + b` for structs with operator+ is a binary_expression in
|
||||
* the grammar. cs_call_types = {invocation_expression} only — no
|
||||
* binary_expression, so the operator call is never extracted. */
|
||||
* green=guard: `operator+` is a Method of Vec2 with a node of its own, and the
|
||||
* constructor call in its body belongs to it. operator_declaration was no
|
||||
* function kind, so the operator had no node and `new Vec2(...)` was attributed
|
||||
* to the file. That misattributed edge plus Run->Dot made CALLS >= 2, so the
|
||||
* count this case asserted before passed without the operator ever existing.
|
||||
* Still open [KNOWN class 12]: the operator site `a + b` in Run is no CALLS
|
||||
* edge to operator+ (cs_call_types has no binary_expression). */
|
||||
TEST(mkc_c2_csharp_operator_plus) {
|
||||
static const MKC_File f[] = {{"Vec.cs",
|
||||
"namespace App {\n"
|
||||
@@ -495,11 +555,65 @@ TEST(mkc_c2_csharp_operator_plus) {
|
||||
" }\n"
|
||||
" }\n"
|
||||
"}\n"}};
|
||||
/* REAL BUG: `(a + b).Dot(b)` should CALLS both Vec2::op_Addition (the `+`)
|
||||
* AND Dot (>=2). Dot resolves (CALLS=1) but the operator `a + b` does not —
|
||||
* C# binary_expression operator desugaring is not modeled (cs_call_types omits
|
||||
* it), so the op_Addition call is missing → CALLS=1 < 2. [KNOWN class 12] */
|
||||
ASSERT_TRUE(mkc_edge(f, 1, "CALLS", 2, "c2/csharp/operator_plus", 0));
|
||||
MKC_Proj lp;
|
||||
cbm_store_t *store = mkc_index(&lp, f, 1);
|
||||
int op_nodes = mkc_label_nodes_by_qn_suffix(store, lp.project, "Method", "Vec2.operator+");
|
||||
int op_ctor =
|
||||
mkc_edges_from_suffix_to_name(store, lp.project, "CALLS", "Vec2.operator+", "Vec2");
|
||||
int run_dot = mkc_edges_from_suffix_to_name(store, lp.project, "CALLS", "Math.Run", "Dot");
|
||||
if (op_nodes != 1 || op_ctor != 1 || run_dot != 1) {
|
||||
fprintf(stderr,
|
||||
" [MKC] c2/csharp/operator_plus FAIL operator_methods=%d operator_ctor_calls=%d "
|
||||
"run_dot_calls=%d (GREEN regression)\n",
|
||||
op_nodes, op_ctor, run_dot);
|
||||
mkc_diag(store, lp.project, "c2/csharp/operator_plus");
|
||||
}
|
||||
mkc_cleanup(&lp, store);
|
||||
ASSERT_EQ(op_nodes, 1);
|
||||
ASSERT_EQ(op_ctor, 1);
|
||||
ASSERT_EQ(run_dot, 1);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* C2-G2: C# — overloaded operators, and operators spelled with a space or a word.
|
||||
* green=guard: the two `operator+` overloads merge into one Method node, as
|
||||
* other C# overloads do; `operator -` is spelled `operator-`, and `operator true`
|
||||
* keeps its space. Each operator owns the calls in its body. */
|
||||
TEST(mkc_c2_csharp_operator_overloads) {
|
||||
static const MKC_File f[] = {
|
||||
{"Vec.cs",
|
||||
"namespace App {\n"
|
||||
" struct Vec2 {\n"
|
||||
" public float X;\n"
|
||||
" public Vec2(float x) { X = x; }\n"
|
||||
" public static Vec2 operator+(Vec2 a, Vec2 b) { return new Vec2(a.X + b.X); }\n"
|
||||
" public static Vec2 operator+(Vec2 a, float b) { return new Vec2(a.X + b); }\n"
|
||||
" public static Vec2 operator -(Vec2 a, Vec2 b) { return new Vec2(a.X - b.X); }\n"
|
||||
" public static bool operator true(Vec2 a) { return Check(a); }\n"
|
||||
" static bool Check(Vec2 a) { return true; }\n"
|
||||
" }\n"
|
||||
"}\n"}};
|
||||
MKC_Proj lp;
|
||||
cbm_store_t *store = mkc_index(&lp, f, 1);
|
||||
int plus_nodes = mkc_label_nodes_by_qn_suffix(store, lp.project, "Method", "Vec2.operator+");
|
||||
int plus_ctor =
|
||||
mkc_edges_from_suffix_to_name(store, lp.project, "CALLS", "Vec2.operator+", "Vec2");
|
||||
int minus_ctor =
|
||||
mkc_edges_from_suffix_to_name(store, lp.project, "CALLS", "Vec2.operator-", "Vec2");
|
||||
int true_check =
|
||||
mkc_edges_from_suffix_to_name(store, lp.project, "CALLS", "Vec2.operator true", "Check");
|
||||
if (plus_nodes != 1 || plus_ctor != 1 || minus_ctor != 1 || true_check != 1) {
|
||||
fprintf(stderr,
|
||||
" [MKC] c2/csharp/operator_overloads FAIL plus_methods=%d plus_ctor=%d "
|
||||
"minus_ctor=%d true_check=%d (GREEN regression)\n",
|
||||
plus_nodes, plus_ctor, minus_ctor, true_check);
|
||||
mkc_diag(store, lp.project, "c2/csharp/operator_overloads");
|
||||
}
|
||||
mkc_cleanup(&lp, store);
|
||||
ASSERT_EQ(plus_nodes, 1);
|
||||
ASSERT_EQ(plus_ctor, 1);
|
||||
ASSERT_EQ(minus_ctor, 1);
|
||||
ASSERT_EQ(true_check, 1);
|
||||
PASS();
|
||||
}
|
||||
|
||||
@@ -1251,8 +1365,9 @@ SUITE(matrix_known_classes) {
|
||||
RUN_TEST(mkc_c2_rust_add_trait);
|
||||
/* C2-F: Kotlin operator fun plus — red=bug */
|
||||
RUN_TEST(mkc_c2_kotlin_operator_plus);
|
||||
/* C2-G: C# operator+ — red=bug */
|
||||
/* C2-G: C# operator+ — green=guard (the `a + b` site is still class 12) */
|
||||
RUN_TEST(mkc_c2_csharp_operator_plus);
|
||||
RUN_TEST(mkc_c2_csharp_operator_overloads);
|
||||
/* C2-H: Scala symbolic method — uncertain/red=bug */
|
||||
RUN_TEST(mkc_c2_scala_symbolic_method);
|
||||
/* C2-I: Ruby def + — uncertain/red=bug */
|
||||
|
||||
@@ -69,9 +69,9 @@ static cbm_path_alias_map_t *make_map(const char *base_url, int count, ...) {
|
||||
return map;
|
||||
}
|
||||
|
||||
/* The map produced by make_map() owns all heap memory the same way the
|
||||
* loader does, so the public free routine on the wrapping collection
|
||||
* works equivalently — but tests build naked maps, so use this helper. */
|
||||
/* make_map() allocates with the C allocator, the loader through the memory
|
||||
* core, so a map built here is freed with this helper and never handed to
|
||||
* cbm_path_alias_collection_free. */
|
||||
static void free_map(cbm_path_alias_map_t *map) {
|
||||
if (!map) {
|
||||
return;
|
||||
@@ -212,7 +212,12 @@ TEST(path_alias_find_for_file_nearest_ancestor) {
|
||||
ASSERT_EQ(m2->count, 1);
|
||||
ASSERT_STR_EQ(m2->entries[0].alias_prefix, "@root/");
|
||||
|
||||
cbm_path_alias_collection_free(coll);
|
||||
for (int i = 0; i < coll->count; i++) {
|
||||
free(coll->scopes[i].dir_prefix);
|
||||
free_map(coll->scopes[i].map);
|
||||
}
|
||||
free(coll->scopes);
|
||||
free(coll);
|
||||
PASS();
|
||||
}
|
||||
|
||||
|
||||
@@ -2848,6 +2848,41 @@ TEST(pipeline_field_named_like_method_keeps_its_reads_and_writes) {
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* C `x++` and `--x` write x. The C assignment node set held only
|
||||
* assignment_expression, so an increment or a decrement left no WRITES edge,
|
||||
* although the write resolver already handles update_expression and `x = 0`
|
||||
* in the same file did write. */
|
||||
TEST(pipeline_c_increment_writes) {
|
||||
char tmp[256];
|
||||
snprintf(tmp, sizeof(tmp), "/tmp/cbm_incwrite_XXXXXX");
|
||||
ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
|
||||
th_write_file(TH_PATH(tmp, "src/counter.c"),
|
||||
"struct counter { int n; };\n"
|
||||
"int hits;\n"
|
||||
"void hits_reset(void) { hits = 0; }\n"
|
||||
"void hits_bump(void) { hits++; }\n"
|
||||
"void hits_drop(void) { --hits; }\n"
|
||||
"void counter_tick(struct counter *c) { c->n++; }\n");
|
||||
char db_path[512];
|
||||
snprintf(db_path, sizeof(db_path), "%s/incwrite.db", tmp);
|
||||
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
|
||||
ASSERT_NOT_NULL(p);
|
||||
ASSERT_EQ(cbm_pipeline_run(p), 0);
|
||||
const char *project = cbm_pipeline_project_name(p);
|
||||
cbm_store_t *store = cbm_store_open_path(db_path);
|
||||
ASSERT_NOT_NULL(store);
|
||||
/* The plain assignment shows the fixture can produce WRITES at all. */
|
||||
ASSERT_EQ(labeled_edge_count(store, project, "WRITES", "hits_reset", "hits", "Variable"), 1);
|
||||
/* Postfix and prefix on a file-level variable, postfix on a member. */
|
||||
ASSERT_EQ(labeled_edge_count(store, project, "WRITES", "hits_bump", "hits", "Variable"), 1);
|
||||
ASSERT_EQ(labeled_edge_count(store, project, "WRITES", "hits_drop", "hits", "Variable"), 1);
|
||||
ASSERT_EQ(labeled_edge_count(store, project, "WRITES", "counter_tick", "n", "Field"), 1);
|
||||
cbm_store_close(store);
|
||||
cbm_pipeline_free(p);
|
||||
th_cleanup(tmp);
|
||||
PASS();
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
int run_rc;
|
||||
bool store_opened;
|
||||
@@ -35032,6 +35067,7 @@ SUITE(pipeline) {
|
||||
RUN_TEST(pipeline_incremental_keeps_ansible_notifies_across_files);
|
||||
RUN_TEST(pipeline_incremental_keeps_ansible_deploys_across_files);
|
||||
RUN_TEST(pipeline_field_named_like_method_keeps_its_reads_and_writes);
|
||||
RUN_TEST(pipeline_c_increment_writes);
|
||||
RUN_TEST(pipeline_objectscript_export_preserves_calls_sequential_parallel);
|
||||
RUN_TEST(pipeline_objectscript_export_incremental_matches_full_relationships);
|
||||
RUN_TEST(pipeline_js_route_not_bound_to_python_builtin_sequential);
|
||||
|
||||
Reference in New Issue
Block a user