feat(lsp): the declared-type step's fields for the further engines

Everything the remaining engine slices need from the shared step lands
at once, so each of them appends rows and touches no shared code:

Type-scope rows state every field. New ones, unused by the Go and Python
rows (their graphs are unchanged):
- scope_source: MODULE_IMPORTS (the own module, then the import map; Go,
  Python), INCLUDE_CLOSURE (a caller-supplied visible() answers a name:
  C and C++ once the include closure exists) or LEXICAL_NESTING (the
  namespaces of the file enclosing the definition, innermost first: Ruby
  constants).
- colon_paths: `a::B` is one spelling whose QN joins with `.`; a leading
  `::` gets no identity.
- open_imports: import-map entries under the key "*" name open packages;
  a name nothing else binds is the one type of that name across them, or
  none.
- implicit_external: a predicate for the language's implicit external
  scope (java.lang and the like); a name it declares gets no identity.
- nested_types: a bare name written inside a type is first that type's
  member type, then each enclosing type's, before the module; member
  types of supertypes are not seen (a possible answer, not a proven one).

The calling file's own table, cbm_type_scope_file_imports: each local its
named imports bind, with the verified type identity, from the same scope
the collector attaches with. A walk reaches it through
cbm_pxc_file_scope(f->ctx, ...); every driver now hands the walks the
pipeline context in CBMCrossLspRegistries.ctx (behaviour unchanged: a
struct with no registry built is read like none).

One row-driven field fold (pxc_fold_fields) replaces
pxc_fold_struct_fields and pxc_fold_py_field_types. The engine row says
where fields come from ("Field" definitions with raw types: Go, C, C++;
annotations resolved to project QNs: Python), the owner label, that only
the last owner of a QN takes them (Python) and which of the row's
languages fold (C and C++, not CUDA: kept as before). One hash of the
owners, one pass over the fields, one chain per owner.

Tests: each new field through the public step API with a hand-made row,
each with its negative partner; the calling-file table (Python, Go, open
key, a star import); cbm_pxc_file_scope per engine; counted work of the
nested and open lookups at two sizes; the fold's rows (Go and C fold in
declaration order, CUDA none). Go and Python graphs are identical to
before on their development repositories, as are C and C++.

Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
This commit is contained in:
Martin Vogel
2026-11-19 00:18:00 +01:00
parent dc5dc25e4a
commit 68d9470151
7 changed files with 1081 additions and 185 deletions
+238 -175
View File
@@ -840,85 +840,42 @@ static int pxc_build_lsp_def(CBMArena *arena, const CBMDefinition *src, const ch
return 0;
}
/* Go and C: fold per-field "Field" definitions into their owning type's
* field_defs. extract_defs.c emits one flat CBMDefinition per struct field
* (label "Field", parent_class = owning struct QN, name = field name,
* return_type = raw type text). Those rows are dropped by pxc_build_lsp_def
* (pxc_map_label excludes "Field"), so without this fold every struct
* registers with zero fields: Go field-chain calls (h.svc.Handle) can never
* resolve, and a C member access on a struct from another file (every header)
* has no owner to bind. Fields are always declared in the same file as their
* struct, so scanning the file's own defs covers every case. Runs inside
/* The file's fields onto their owner types (field_defs "name:type|...", the
* text the registrars read), as the language's engine row describes them
* (pxc_engine_t field_*); defined with the engine table below. Runs inside
* cbm_pxc_collect_all_defs — one site covers both the prebuilt-registry path
* and the per-file fallback, since both consume all_defs.
*
* type_label is the label the language's aggregate carries: "Struct" for Go,
* "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 || result->defs.count <= 0) {
return;
* and the per-file fallback, since both consume all_defs. */
static void pxc_fold_fields(CBMArena *arena, const CBMFileResult *fr, CBMLSPDef *defs, int start,
int end, CBMLanguage lang, const pxc_base_scope_t *bs,
const char *module_qn);
/* One file's declared-type scope (type_scope.h): the project registry, the
* file's module and LSP import map, and for a lexical row the file's
* definitions and module facts. The collector attaches the file's identities
* with it, and a walk reads the calling file's own identities from the same
* scope (cbm_pxc_file_scope). */
static cbm_type_scope_file_t *pxc_file_scope_begin(const cbm_pipeline_ctx_t *ctx,
const cbm_type_scope_lang_t *row,
const CBMFileResult *fr, const char *module_qn,
const char **imp_keys, const char **imp_vals,
int imp_count) {
if (!ctx || !ctx->registry || !row || !fr) {
return NULL;
}
/* 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));
cbm_type_scope_t scope = {.reg = ctx->registry,
.module_qn = module_qn,
.imp_keys = imp_keys,
.imp_vals = imp_vals,
.imp_count = imp_count};
if (row->lexical_scopes) {
/* The file's own scopes: its definitions and the module's ordered
* namespace facts. */
scope.file_defs = fr->defs.items;
scope.file_def_count = fr->defs.count;
scope.module_facts = &fr->py_namespace;
scope.graph = ctx->gbuf; /* the graph the import map read */
}
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 */
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++;
}
if (count == 0) {
continue;
}
/* count - 1 separators + NUL. */
size_t bufsz = total + (size_t)(count - 1) + 1;
char *buf = (char *)cbm_arena_alloc(arena, bufsz);
if (!buf) {
continue;
}
char *p = buf;
int written = 0;
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;
*p++ = ':';
n = strlen(fd->return_type);
memcpy(p, fd->return_type, n);
p += n;
if (written + 1 < count) {
*p++ = '|';
}
written++;
}
*p = '\0';
dst->field_defs = buf;
}
cbm_ht_free(fields_of);
cbm_free(CBM_MEM_CLASS_EXTRACT, next_field);
return cbm_type_scope_file_begin(row, &scope);
}
/* Python annotation text -> the one type name it declares, or NULL. Strips a
@@ -972,79 +929,6 @@ static const char *pxc_py_annotation_type_name(CBMArena *arena, const char *text
return cbm_arena_strndup(arena, text, n);
}
/* Python: fold the file's annotated instance fields (result->field_types,
* #1277) into their class defs' field_defs as "name:QN|name:QN", the format
* py_register_lsp_defs reads. Each annotation resolves through the same
* registry + import map, and the same weak-strategy and type-like vetoes, as
* the file's base classes (pxc_resolve_base_qn): a field whose type is not a
* confidently-known project type is left out rather than guessed. Linear in
* the file: one hash of the file's class defs, one pass over its fields. */
typedef struct {
int owner; /* index into defs[], or -1 */
const char *type_qn;
} pxc_py_field_t;
static void pxc_fold_py_field_types(CBMArena *arena, const CBMFileResult *result, CBMLSPDef *defs,
int start, int end, const pxc_base_scope_t *bs,
const char *module_qn) {
int nf = result ? result->field_types.count : 0;
if (!arena || nf == 0 || !defs || start >= end || !bs || !bs->reg) {
return;
}
CBMHashTable *by_qn = cbm_ht_create((uint32_t)(end - start));
pxc_py_field_t *fields = (pxc_py_field_t *)cbm_arena_alloc(arena, (size_t)nf * sizeof(*fields));
/* Per class def: exact buffer size, then the write cursor into it. */
size_t *lens = (size_t *)cbm_arena_alloc(arena, (size_t)(end - start) * sizeof(size_t));
size_t *used = (size_t *)cbm_arena_alloc(arena, (size_t)(end - start) * sizeof(size_t));
if (!by_qn || !fields || !lens || !used) {
cbm_ht_free(by_qn); /* NULL-safe */
return;
}
memset(lens, 0, (size_t)(end - start) * sizeof(size_t));
memset(used, 0, (size_t)(end - start) * sizeof(size_t));
for (int si = start; si < end; si++) {
if (defs[si].label && strcmp(defs[si].label, "Class") == 0 && defs[si].qualified_name) {
cbm_ht_set(by_qn, defs[si].qualified_name, &defs[si]);
}
}
for (int f = 0; f < nf; f++) {
const CBMFieldType *ft = &result->field_types.items[f];
fields[f].owner = -1;
fields[f].type_qn = NULL;
CBMLSPDef *owner = ft->class_qn ? (CBMLSPDef *)cbm_ht_get(by_qn, ft->class_qn) : NULL;
const char *name = owner ? pxc_py_annotation_type_name(arena, ft->type_text) : NULL;
const char *qn = name ? pxc_resolve_base_qn(bs, name, module_qn, NULL) : NULL;
if (!qn || !ft->field_name || !ft->field_name[0]) {
continue;
}
fields[f].owner = (int)(owner - defs);
fields[f].type_qn = qn;
/* "name:QN" plus one byte for the '|' or the NUL after it. */
lens[fields[f].owner - start] += strlen(ft->field_name) + 1 + strlen(qn) + 1;
}
cbm_ht_free(by_qn);
for (int si = start; si < end; si++) {
char *buf = lens[si - start] ? (char *)cbm_arena_alloc(arena, lens[si - start]) : NULL;
if (buf) {
buf[0] = '\0';
}
defs[si].field_defs = buf;
}
for (int f = 0; f < nf; f++) {
char *buf = fields[f].owner >= 0 ? (char *)defs[fields[f].owner].field_defs : NULL;
if (!buf) {
continue;
}
int slot = fields[f].owner - start;
int wrote =
snprintf(buf + used[slot], lens[slot] - used[slot], "%s%s:%s", used[slot] ? "|" : "",
result->field_types.items[f].field_name, fields[f].type_qn);
if (wrote > 0) {
used[slot] += (size_t)wrote;
}
}
}
/* Go: a generic type's field_defs ("name:type|...") with its type parameters
* masked in the type parts (pxc_go_mask_type_params), never in the names. */
static const char *pxc_go_mask_field_types(CBMArena *arena, const char *field_defs,
@@ -1547,8 +1431,9 @@ CBMLSPDef *cbm_pxc_collect_all_defs(const cbm_pipeline_ctx_t *ctx, CBMArena *are
idx++;
}
}
/* The file's fields onto their owner types, as the engine row says. */
pxc_fold_fields(arena, fr, defs, file_start, idx, files[fi].language, &bs, def_modules[fi]);
if (files[fi].language == CBM_LANG_PYTHON) {
pxc_fold_py_field_types(arena, fr, defs, file_start, idx, &bs, def_modules[fi]);
/* Package namespace records (re-exports), reserved in the bound
* above; written only while they fit it (a result whose loaded
* stream outgrew its header would otherwise overflow). */
@@ -1557,13 +1442,6 @@ CBMLSPDef *cbm_pxc_collect_all_defs(const cbm_pipeline_ctx_t *ctx, CBMArena *are
idx += pxc_build_py_namespace_records(arena, fr, def_modules[fi], &defs[idx]);
}
}
if (files[fi].language == CBM_LANG_GO) {
pxc_fold_struct_fields(arena, fr, defs, file_start, idx, "Struct");
} else if (files[fi].language == CBM_LANG_C || files[fi].language == CBM_LANG_CPP) {
/* Both: a `.h` file is classified C++, and that is where C declares
* the structs its `.c` files access. */
pxc_fold_struct_fields(arena, fr, defs, file_start, idx, "Class");
}
/* Go generic types and Python class bodies (CBMDefTypeFacts). */
pxc_apply_type_facts(arena, fr, defs, file_start, idx, files[fi].language);
if (files[fi].language == CBM_LANG_RUST) {
@@ -1578,20 +1456,8 @@ CBMLSPDef *cbm_pxc_collect_all_defs(const cbm_pipeline_ctx_t *ctx, CBMArena *are
/* Every declared type this file wrote, resolved once in this file's
* scope; the identities travel with the defs (and their surface). */
if (scope_lang) {
cbm_type_scope_t scope = {.reg = ctx->registry,
.module_qn = def_modules[fi],
.imp_keys = bs.imp_keys,
.imp_vals = bs.imp_vals,
.imp_count = bs.imp_count};
if (scope_lang->lexical_scopes) {
/* The file's own scopes: its definitions and the module's
* ordered namespace facts. */
scope.file_defs = fr->defs.items;
scope.file_def_count = fr->defs.count;
scope.module_facts = &fr->py_namespace;
scope.graph = ctx->gbuf; /* the graph the import map read */
}
cbm_type_scope_file_t *ts_file = cbm_type_scope_file_begin(scope_lang, &scope);
cbm_type_scope_file_t *ts_file = pxc_file_scope_begin(
ctx, scope_lang, fr, def_modules[fi], bs.imp_keys, bs.imp_vals, bs.imp_count);
for (int di = file_start; ts_file && di < idx; di++) {
cbm_type_scope_attach(ts_file, arena, &defs[di]);
}
@@ -2361,6 +2227,10 @@ typedef struct {
const CBMModuleDefIndex *module_def_index;
CBMLSPDef *all_defs;
int all_def_count;
/* The pipeline context (CBMCrossLspRegistries.ctx; NULL on the
* cbm_pxc_run_one route): a walk reads the calling file's own identities
* through cbm_pxc_file_scope(f->ctx, ...). */
const cbm_pipeline_ctx_t *ctx;
} pxc_file_t;
/* One walk: where it resolves (`reg`, the per-file overlay chained to the
@@ -2384,6 +2254,27 @@ typedef bool (*pxc_walk_fn)(const pxc_file_t *f, pxc_walk_t *w);
enum { PXC_ENGINE_LANGS = 3 };
/* Where a file's fields come from. */
typedef enum {
PXC_FIELDS_NONE = 0,
/* "Field" definitions (parent_class = the owner QN, name, return_type =
* the raw type text). extract_defs.c emits one flat CBMDefinition per
* struct field; pxc_build_lsp_def drops them (pxc_map_label excludes
* "Field"), so without the fold every struct registers with zero fields:
* Go field-chain calls (h.svc.Handle) can never resolve, and a C member
* access on a struct from another file (every header) has no owner to
* bind. Fields are always declared in the same file as their struct. */
PXC_FIELDS_DEFS,
/* The file's annotated instance fields (CBMFileResult.field_types,
* #1277), each annotation reduced to the one type it names
* (pxc_py_annotation_type_name) and resolved through the same registry,
* import map, weak-strategy and type-like vetoes as the file's base
* classes (pxc_resolve_base_qn): a field whose type is not a
* confidently-known project type is left out rather than guessed. Needs
* the pipeline registry (none on the surface-probe path). */
PXC_FIELDS_ANNOTATIONS,
} pxc_field_source_t;
struct pxc_engine {
const char *name; /* the registry-build log name */
CBMLanguage langs[PXC_ENGINE_LANGS];
@@ -2405,6 +2296,16 @@ struct pxc_engine {
* attaches them to the language's defs and the per-file selection follows
* them. The language has a cbm_type_scope_lang row. */
bool wants_identities;
/* The fold of a file's fields into their owner type's field_defs
* ("name:type|..."), the text the registrars read (pxc_fold_fields). */
pxc_field_source_t field_source;
const char *field_owner_label; /* the owners' label */
/* Only the last owner definition of a QN takes the fields (Python: a
* class defined twice in one file); else every one of that QN does. */
bool field_owner_last;
/* The row's languages whose files fold (0: every one of them). */
CBMLanguage field_langs[PXC_ENGINE_LANGS];
int field_lang_count;
};
/* ── Go ── */
@@ -2611,20 +2512,32 @@ static const pxc_engine_t pxc_engines[] = {
.build_registry = cbm_go_build_cross_registry,
.run_with_registry = pxc_go_run_registry,
.run_one = pxc_go_run_defs,
.wants_identities = true},
.wants_identities = true,
.field_source = PXC_FIELDS_DEFS,
.field_owner_label = "Struct"},
{.name = "python",
.langs = {CBM_LANG_PYTHON},
.lang_count = 1,
.build_registry = cbm_py_build_cross_registry,
.run_with_registry = pxc_py_run_registry,
.run_one = pxc_py_run_defs,
.wants_identities = true},
.wants_identities = true,
.field_source = PXC_FIELDS_ANNOTATIONS,
.field_owner_label = "Class",
.field_owner_last = true},
/* A C struct or union is a "Class". C and C++ files fold their fields
* (a `.h` file is classified C++, and that is where C declares the
* structs its `.c` files access); CUDA files fold none. */
{.name = "c",
.langs = {CBM_LANG_C, CBM_LANG_CPP, CBM_LANG_CUDA},
.lang_count = 3,
.build_registry = cbm_c_build_cross_registry,
.run_with_registry = pxc_c_run_registry,
.run_one = pxc_c_run_defs},
.run_one = pxc_c_run_defs,
.field_source = PXC_FIELDS_DEFS,
.field_owner_label = "Class",
.field_langs = {CBM_LANG_C, CBM_LANG_CPP},
.field_lang_count = 2},
/* C# has no per-file walk: without its shared registry a C# file gets no
* cross-file resolution. */
{.name = "cs",
@@ -2719,6 +2632,154 @@ CBMTypeRegistry *cbm_pxc_registry_for_lang(const CBMCrossLspRegistries *r, CBMLa
return e >= 0 ? r->by_engine[e] : NULL;
}
cbm_type_scope_file_t *cbm_pxc_file_scope(const cbm_pipeline_ctx_t *ctx, CBMLanguage lang,
const CBMFileResult *result, const char *module_qn,
const char **imp_keys, const char **imp_vals,
int imp_count) {
if (!cbm_pxc_wants_identities(lang)) {
return NULL;
}
return pxc_file_scope_begin(ctx, cbm_type_scope_lang(lang), result, module_qn, imp_keys,
imp_vals, imp_count);
}
/* ── The field fold ── */
/* One field of the file, in source order. */
typedef struct {
const char *owner; /* the owner type's QN */
const char *name;
const char *type; /* the raw text (DEFS) or a resolved QN (ANNOTATIONS) */
int next; /* 1-based index of the owner's next field; 0 = its last */
} pxc_field_item_t;
static bool pxc_engine_folds(const pxc_engine_t *e, CBMLanguage lang) {
if (!e || e->field_source == PXC_FIELDS_NONE || !e->field_owner_label) {
return false;
}
bool listed = e->field_lang_count == 0;
for (int i = 0; i < e->field_lang_count; i++) {
listed = listed || e->field_langs[i] == lang;
}
return listed;
}
/* The fields of one file, whose owner is one of the file's owner definitions,
* in source order; their count. */
static int pxc_field_items(CBMArena *arena, const CBMFileResult *fr, const pxc_engine_t *e,
const CBMHashTable *owners, const pxc_base_scope_t *bs,
const char *module_qn, pxc_field_item_t *items) {
int n = 0;
if (e->field_source == PXC_FIELDS_DEFS) {
for (int di = 0; di < fr->defs.count; di++) {
const CBMDefinition *fd = &fr->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 ||
!cbm_ht_get(owners, fd->parent_class)) {
continue;
}
items[n++] = (pxc_field_item_t){fd->parent_class, fd->name, fd->return_type, 0};
}
return n;
}
for (int f = 0; f < fr->field_types.count; f++) {
const CBMFieldType *ft = &fr->field_types.items[f];
if (!ft->class_qn || !cbm_ht_get(owners, ft->class_qn)) {
continue;
}
const char *name = pxc_py_annotation_type_name(arena, ft->type_text);
const char *qn = name ? pxc_resolve_base_qn(bs, name, module_qn, NULL) : NULL;
if (qn && ft->field_name && ft->field_name[0]) {
items[n++] = (pxc_field_item_t){ft->class_qn, ft->field_name, qn, 0};
}
}
return n;
}
/* Fold the file's fields into their owner definitions' field_defs as
* "name:type|name:type", each owner's fields in source order. One hash of the
* file's owner definitions, one pass over its fields, one chain per owner:
* linear in the file. An owner without fields keeps its field_defs. */
static void pxc_fold_fields(CBMArena *arena, const CBMFileResult *fr, CBMLSPDef *defs, int start,
int end, CBMLanguage lang, const pxc_base_scope_t *bs,
const char *module_qn) {
const pxc_engine_t *e = pxc_engine_for(lang);
if (!arena || !fr || !defs || start >= end || !pxc_engine_folds(e, lang)) {
return;
}
bool annotations = e->field_source == PXC_FIELDS_ANNOTATIONS;
int cap = annotations ? fr->field_types.count : fr->defs.count;
if (cap <= 0 || (annotations && (!bs || !bs->reg))) {
return;
}
/* owner QN -> 1-based index of its owner definition (the last one with
* field_owner_last; any one otherwise, each is matched below by QN) */
CBMHashTable *owners = cbm_ht_create_in(CBM_MEM_CLASS_EXTRACT, (uint32_t)(end - start + 1));
CBMHashTable *chains = cbm_ht_create_in(CBM_MEM_CLASS_EXTRACT, 32);
pxc_field_item_t *items = cbm_alloc(CBM_MEM_CLASS_EXTRACT, (size_t)cap * sizeof(*items));
if (!owners || !chains || !items) {
cbm_ht_free(owners); /* NULL-safe */
cbm_ht_free(chains);
cbm_free(CBM_MEM_CLASS_EXTRACT, items);
return;
}
for (int si = start; si < end; si++) {
if (defs[si].label && defs[si].qualified_name &&
strcmp(defs[si].label, e->field_owner_label) == 0) {
cbm_ht_set(owners, defs[si].qualified_name, (void *)(intptr_t)(si + 1));
}
}
int n = pxc_field_items(arena, fr, e, owners, bs, module_qn, items);
/* Chain each owner's fields in source order (prepend, walking back). */
for (int i = n - 1; i >= 0; i--) {
items[i].next =
(int)(intptr_t)cbm_ht_set(chains, items[i].owner, (void *)(intptr_t)(i + 1));
}
for (int si = start; si < end; si++) {
CBMLSPDef *dst = &defs[si];
if (!dst->label || !dst->qualified_name || strcmp(dst->label, e->field_owner_label) != 0 ||
(e->field_owner_last &&
(int)(intptr_t)cbm_ht_get(owners, dst->qualified_name) != si + 1)) {
continue;
}
int first = (int)(intptr_t)cbm_ht_get(chains, dst->qualified_name);
int count = 0;
size_t total = 0; /* "name:type" bytes; separators and NUL added below */
for (int link = first; link; link = items[link - 1].next) {
cbm_resolve_stats_add(CBM_RESOLVE_STAT_LOOKUP_PROBES, 1);
total += strlen(items[link - 1].name) + 1 + strlen(items[link - 1].type);
count++;
}
if (count == 0) {
continue;
}
/* count - 1 separators + NUL. */
char *buf = (char *)cbm_arena_alloc(arena, total + (size_t)(count - 1) + 1);
if (!buf) {
continue;
}
char *p = buf;
for (int link = first; link; link = items[link - 1].next) {
const pxc_field_item_t *it = &items[link - 1];
size_t len = strlen(it->name);
memcpy(p, it->name, len);
p += len;
*p++ = ':';
len = strlen(it->type);
memcpy(p, it->type, len);
p += len;
if (it->next) {
*p++ = '|';
}
}
*p = '\0';
dst->field_defs = buf;
}
cbm_ht_free(owners);
cbm_ht_free(chains);
cbm_free(CBM_MEM_CLASS_EXTRACT, items);
}
/* One walk in its own scratch arena; its output joins the file's result
* unless the walk's proof failed. */
static void pxc_walk_registry(const pxc_engine_t *e, const pxc_file_t *f, CBMTypeRegistry *shared,
@@ -2893,7 +2954,8 @@ void cbm_pxc_dispatch_file_with_php_bindings(
.php_bindings = bindings,
.module_def_index = module_def_index,
.all_defs = all_defs,
.all_def_count = all_def_count};
.all_def_count = all_def_count,
.ctx = cross_registries ? cross_registries->ctx : NULL};
CBMTypeRegistry *prebuilt = cbm_pxc_registry_for_lang(cross_registries, lang);
if (prebuilt && e && e->run_with_registry) {
pxc_walk_registry(e, &f, prebuilt, true);
@@ -3079,6 +3141,7 @@ int cbm_pipeline_pass_lsp_cross(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *
all_defs ? cbm_pxc_build_module_def_index(all_defs, def_count) : NULL;
CBMCrossLspRegistries cross_registries;
memset(&cross_registries, 0, sizeof(cross_registries));
cross_registries.ctx = ctx;
if (all_defs) {
CBMArena *xa = &ctx->seq_cross_arena;
if (!ctx->seq_cross_arena_live) {
+17
View File
@@ -39,6 +39,7 @@
#include "lsp/java_lsp.h" /* cbm_java_build_cross_registry / cbm_run_java_lsp_cross_with_registry */
#include "lsp/rust_lsp.h" /* cbm_rust_build_cross_registry / cbm_run_rust_lsp_cross_with_registry */
#include "pipeline/pipeline_internal.h"
#include "pipeline/type_scope.h" /* cbm_type_scope_file_t */
#include <stdbool.h>
/* ── The engine table ──
@@ -64,6 +65,18 @@ bool cbm_pxc_has_cross_lsp(CBMLanguage lang);
* row) and the per-file def selection follows them. */
bool cbm_pxc_wants_identities(CBMLanguage lang);
/* One file's declared-type scope, exactly as the collector builds it to
* attach the file's identities (the project registry, the file's module and
* LSP import map, and for a lexical row the file's definitions and module
* facts): a walk reads the calling file's own identity table from it
* (cbm_type_scope_file_imports). NULL without a pipeline context or for an
* engine that reads no identities; else end it with cbm_type_scope_file_end
* once the walk is done. */
cbm_type_scope_file_t *cbm_pxc_file_scope(const cbm_pipeline_ctx_t *ctx, CBMLanguage lang,
const CBMFileResult *result, const char *module_qn,
const char **imp_keys, const char **imp_vals,
int imp_count);
/* Collect a project-wide CBMLSPDef[] from every cached file result.
* def_modules[i] receives the module QN for files[i] (malloc'd; the
* caller frees each entry then the array). String fields in the
@@ -171,6 +184,10 @@ uint64_t cbm_pxc_test_reach_work(void);
* parallel resolver builds it on the first file that needs it. */
typedef struct {
CBMTypeRegistry *by_engine[CBM_PXC_MAX_ENGINES]; /* indexed by engine row */
/* The pipeline context the walks may read (the project registry, for the
* calling file's own identities: cbm_pxc_file_scope). Every driver sets
* it, also when it builds no registry; NULL in a harness without one. */
const cbm_pipeline_ctx_t *ctx;
} CBMCrossLspRegistries;
/* Build one row's shared registry over all defs into out->by_engine[engine].
+1
View File
@@ -3116,6 +3116,7 @@ static int run_parallel_pipeline(cbm_pipeline_t *p, cbm_pipeline_ctx_t *ctx,
* all filter to subsets never pay the build/RSS. */
CBMCrossLspRegistries cross_registries;
memset(&cross_registries, 0, sizeof(cross_registries));
cross_registries.ctx = ctx;
if (all_defs) {
/* Per-builder split of lsp_cross_prepare — attributes a slow prepare to
* ONE engine instead of re-diagnosing the whole pass (the cs builder
+4 -3
View File
@@ -2002,9 +2002,11 @@ static int run_extract_resolve(cbm_pipeline_ctx_t *ctx, cbm_file_info_t *changed
int all_def_count = 0;
char **def_modules = NULL;
CBMModuleDefIndex *module_def_index = NULL;
/* Every route hands the walks the context; only a closure above the
* floor builds shared registries into it. */
CBMCrossLspRegistries cross_registries;
memset(&cross_registries, 0, sizeof(cross_registries));
CBMCrossLspRegistries *registries_arg = NULL;
cross_registries.ctx = ctx;
CBMArena legacy_cross_arena;
bool legacy_cross_arena_live = false;
bool owner_walk_required = atomic_load(&ctx->test_definition_owners_seen) != 0;
@@ -2077,7 +2079,6 @@ static int run_extract_resolve(cbm_pipeline_ctx_t *ctx, cbm_file_info_t *changed
if (ci > CLOSURE_BUDGET_FLOOR_FILES) {
cbm_pxc_build_cross_registries(&closure->arena, all_defs, all_def_count,
&cross_registries);
registries_arg = &cross_registries;
}
} else {
all_def_count = 0;
@@ -2116,7 +2117,7 @@ static int run_extract_resolve(cbm_pipeline_ctx_t *ctx, cbm_file_info_t *changed
cbm_doclinks_begin(ctx, changed_files, ci, cache);
rc = cbm_parallel_resolve(
ctx, changed_files, ci, cache, &shared_ids, worker_count, all_defs, all_def_count,
closure ? closure->def_modules : def_modules, module_def_index, registries_arg);
closure ? closure->def_modules : def_modules, module_def_index, &cross_registries);
cbm_doclinks_end(ctx);
cross_resolve_cleanup:
+313 -6
View File
@@ -28,22 +28,39 @@ static const char *const go_labels[] = {"Struct", "Interface", "Type", "Class",
/* Python: what py_register_lsp_defs registers as a type. */
static const char *const py_labels[] = {"Class", "Interface", "Protocol", "Type", NULL};
/* One row per language whose engine reads identities. A row states every
* field; a further language plugs in by appending its row. */
static const cbm_type_scope_lang_t type_scope_langs[] = {
{.lang = CBM_LANG_GO,
.scope_source = CBM_TYPE_SCOPE_MODULE_IMPORTS,
.quoted_names = false,
.colon_paths = false,
.labels = go_labels,
.engine_qualifies_own_module = true,
.imports_bind_modules = true,
.lexical_scopes = false,
.open_imports = false, /* a dot import binds "."; no spelling names it */
.implicit_external = NULL,
.nested_types = false,
.return_types = true,
.param_types = true,
.field_types = true,
.embedded_types = true},
{.lang = CBM_LANG_PYTHON,
.scope_source = CBM_TYPE_SCOPE_MODULE_IMPORTS,
.quoted_names = true,
.colon_paths = false,
.labels = py_labels,
.engine_qualifies_own_module = false,
.imports_bind_modules = false,
.lexical_scopes = true,
.open_imports = false, /* a star import closes the file (module facts) */
.implicit_external = NULL,
.nested_types = false, /* a class body's names shadow: no identity */
.return_types = true,
.param_types = true},
.param_types = true,
.field_types = false, /* folded as resolved QNs (pass_lsp_cross.c) */
.embedded_types = false /* bases arrive as resolved QNs */},
};
const cbm_type_scope_lang_t *cbm_type_scope_lang(CBMLanguage lang) {
@@ -112,7 +129,21 @@ struct cbm_type_scope_file {
bool closed; /* the module's bindings cannot be listed: no identity */
uintptr_t serial; /* the def being attached, from 1 */
const char *enclosing_class; /* the def's immediately enclosing class, or NULL */
CBMArena keys; /* memo keys and candidate strings */
/* open_imports only: the packages the file's open imports name, each once,
* in import-map order. */
const char **open;
int open_count;
/* nested_types and lexical nesting: "<enclosing QN>\x1f<name>" -> the
* answer through that enclosing scope and its outer ones (a QN, or
* TYPE_SCOPE_MISS). */
CBMHashTable *nest_memo;
const char *nest_start; /* the def being attached: its innermost enclosing scope */
/* the calling-file table (cbm_type_scope_file_imports), built once */
const char **fi_locals;
const char **fi_qns;
int fi_count;
bool fi_built;
CBMArena keys; /* memo keys and candidate strings */
};
static bool type_scope_function_label(const char *label) {
@@ -336,8 +367,14 @@ cbm_type_scope_file_t *cbm_type_scope_file_begin(const cbm_type_scope_lang_t *la
f->imports = cbm_ht_create_in(CBM_MEM_CLASS_EXTRACT, (uint32_t)(scope->imp_count + 1));
f->memo = cbm_ht_create_in(CBM_MEM_CLASS_EXTRACT, 32);
f->seen = cbm_ht_create_in(CBM_MEM_CLASS_EXTRACT, 32);
bool nests = lang->nested_types || lang->scope_source == CBM_TYPE_SCOPE_LEXICAL_NESTING;
f->nest_memo = nests ? cbm_ht_create_in(CBM_MEM_CLASS_EXTRACT, 32) : NULL;
cbm_arena_init_lazy(&f->keys, CBM_SZ_4K);
if (!f->imports || !f->memo || !f->seen ||
if (lang->open_imports && scope->imp_count > 0) {
f->open = cbm_arena_alloc(&f->keys, (size_t)scope->imp_count * sizeof(*f->open));
}
if (!f->imports || !f->memo || !f->seen || (nests && !f->nest_memo) ||
(lang->open_imports && scope->imp_count > 0 && !f->open) ||
(lang->lexical_scopes && !type_scope_index_file_defs(f))) {
cbm_type_scope_file_end(f);
return NULL;
@@ -349,6 +386,15 @@ cbm_type_scope_file_t *cbm_type_scope_file_begin(const cbm_type_scope_lang_t *la
continue;
}
TYPE_SCOPE_WORK(1);
if (lang->open_imports && strcmp(key, CBM_TYPE_SCOPE_OPEN_KEY) == 0) {
/* An open import: its package, once (one probe of the memo). */
char *dedupe = cbm_arena_sprintf(&f->keys, "\x1f%s", val);
if (dedupe && !cbm_ht_get(f->memo, dedupe)) {
cbm_ht_set(f->memo, dedupe, (void *)val);
f->open[f->open_count++] = val;
}
continue;
}
void *prev = cbm_ht_get(f->imports, key);
if (!prev) {
cbm_ht_set(f->imports, key, (void *)val);
@@ -373,6 +419,7 @@ void cbm_type_scope_file_end(cbm_type_scope_file_t *file) {
cbm_ht_free(file->tparams);
cbm_ht_free(file->nesting);
cbm_ht_free(file->bindings);
cbm_ht_free(file->nest_memo);
cbm_arena_destroy(&file->keys);
cbm_free(CBM_MEM_CLASS_EXTRACT, file);
}
@@ -447,6 +494,116 @@ static const char *type_scope_via_import(cbm_type_scope_file_t *f, const char *d
return direct ? direct : member;
}
/* open_imports / implicit_external: the one target `<package>.<dotted>` names
* across the file's open packages, for a name the own module and the named
* imports leave unbound. A head the own module declares (any label) shadows
* the open imports; a head the implicit external scope declares, and two
* different targets, are no answer. */
static const char *type_scope_via_open(cbm_type_scope_file_t *f, const char *dotted,
size_t head_len) {
const char *mod = f->scope.module_qn;
int scope_len = (int)f->scope_len;
const char *head_qn =
cbm_arena_sprintf(&f->keys, "%.*s.%.*s", scope_len, mod, (int)head_len, dotted);
TYPE_SCOPE_WORK(1);
if (!head_qn || cbm_registry_exists(f->scope.reg, head_qn)) {
return NULL;
}
if (f->lang->implicit_external && f->lang->implicit_external(dotted, head_len)) {
return NULL;
}
const char *found = NULL;
for (int i = 0; i < f->open_count; i++) {
const char *qn = cbm_arena_sprintf(&f->keys, "%s.%s", f->open[i], dotted);
const char *hit = qn ? type_scope_target(f, qn) : NULL;
if (hit && found && strcmp(hit, found) != 0) {
return NULL;
}
found = hit ? hit : found;
}
return found;
}
/* CBM_TYPE_SCOPE_INCLUDE_CLOSURE: the head through the caller's include
* closure, the rest named inside it. */
static const char *type_scope_via_visible(cbm_type_scope_file_t *f, const char *dotted,
size_t head_len) {
if (!f->scope.visible || head_len == 0) {
return NULL;
}
TYPE_SCOPE_WORK(1);
const char *head = f->scope.visible(f->scope.visible_ud, dotted, head_len);
if (!head) {
return NULL;
}
if (dotted[head_len] != '.') {
return type_scope_target(f, head);
}
const char *qn = cbm_arena_sprintf(&f->keys, "%s.%s", head, dotted + head_len + 1);
return qn ? type_scope_target(f, qn) : NULL;
}
/* Nested scopes (nested_types, lexical nesting): the innermost scope, from
* `start` outward through its QN prefixes no shorter than `min_len` bytes,
* that declares `head` ("<scope>.<head>" names a project definition of any
* label), or NULL. Memoized per (scope, head) for every scope the walk passes,
* so a name written at nesting depth d costs at most d probes once per
* file. */
static const char *type_scope_declaring_scope(cbm_type_scope_file_t *f, const char *start,
size_t min_len, const char *head, size_t head_len) {
const char **passed = NULL; /* memo keys walked through, not yet answered */
int count = 0;
int cap = 0;
const char *answer = NULL;
size_t cur_len = start ? strlen(start) : 0;
while (cur_len >= min_len && cur_len > 0) {
char *key =
cbm_arena_sprintf(&f->keys, "%.*s\x1f%.*s", (int)cur_len, start, (int)head_len, head);
TYPE_SCOPE_WORK(cur_len + head_len + 1);
if (!key) {
break;
}
void *known = cbm_ht_get(f->nest_memo, key);
if (known) {
answer = known == TYPE_SCOPE_MISS ? NULL : (const char *)known;
break;
}
char *qn =
cbm_arena_sprintf(&f->keys, "%.*s.%.*s", (int)cur_len, start, (int)head_len, head);
if (qn && cbm_registry_exists(f->scope.reg, qn)) {
answer = cbm_arena_strndup(&f->keys, start, cur_len);
cbm_ht_set(f->nest_memo, key, answer ? (void *)answer : TYPE_SCOPE_MISS);
break;
}
if (count == cap) {
int grown_cap = cap ? cap * 2 : 8;
const char **grown =
(const char **)cbm_arena_alloc(&f->keys, (size_t)grown_cap * sizeof(*grown));
if (!grown) {
break;
}
if (count) {
memcpy(grown, passed, (size_t)count * sizeof(*grown));
}
passed = grown;
cap = grown_cap;
}
passed[count++] = key;
const char *dot = NULL;
for (size_t i = cur_len; i > 0; i--) {
if (start[i - 1] == '.') {
dot = start + i - 1;
break;
}
}
cur_len = dot ? (size_t)(dot - start) : 0;
}
for (int i = 0; i < count; i++) {
cbm_ht_set(f->nest_memo, passed[i], answer ? (void *)answer : TYPE_SCOPE_MISS);
}
return answer;
}
/* The memo / seen key of a spelling: the spelling itself, NUL-terminated in a
* caller buffer (a name too long for it has no identity). */
static bool type_scope_key(const char *spelling, size_t len, char *buf, size_t bufsz) {
@@ -463,6 +620,34 @@ static size_t type_scope_head_len(const char *spelling, size_t len) {
return dot ? (size_t)(dot - spelling) : len;
}
/* The spelling with its parts joined by `.` (colon_paths: `a::B` is
* `a.B`), NUL-terminated in the file's arena; *out_len its length. */
static const char *type_scope_dotted(cbm_type_scope_file_t *f, const char *spelling, size_t len,
size_t *out_len) {
char *out = cbm_arena_strndup(&f->keys, spelling, len);
size_t n = len;
if (out && f->lang->colon_paths) {
size_t at = 0;
for (size_t i = 0; i < len; i++) {
if (spelling[i] == ':' && i + 1 < len && spelling[i + 1] == ':') {
out[at++] = '.';
i++;
} else {
out[at++] = spelling[i];
}
}
out[at] = '\0';
n = at;
}
*out_len = n;
return out;
}
/* Lexical nesting resolves in the definition's scopes only (the attach). */
static bool type_scope_needs_def(const cbm_type_scope_lang_t *lang) {
return lang->scope_source == CBM_TYPE_SCOPE_LEXICAL_NESTING;
}
const char *cbm_type_scope_resolve(cbm_type_scope_file_t *f, const char *spelling, size_t len) {
char probe[CBM_SZ_512];
if (!f || !spelling || !type_scope_key(spelling, len, probe, sizeof(probe))) {
@@ -478,8 +663,19 @@ const char *cbm_type_scope_resolve(cbm_type_scope_file_t *f, const char *spellin
return NULL;
}
const char *result = NULL;
const char *dotted = key; /* `T` or `pkg.T`, NUL-terminated */
size_t head_len = type_scope_head_len(dotted, len);
size_t dotted_len = len;
/* `T` or `pkg.T`, NUL-terminated */
const char *dotted = f->lang->colon_paths ? type_scope_dotted(f, key, len, &dotted_len) : key;
if (!dotted || type_scope_needs_def(f->lang)) {
cbm_ht_set(f->memo, key, TYPE_SCOPE_MISS);
return NULL;
}
size_t head_len = type_scope_head_len(dotted, dotted_len);
if (f->lang->scope_source == CBM_TYPE_SCOPE_INCLUDE_CLOSURE) {
result = type_scope_via_visible(f, dotted, head_len);
cbm_ht_set(f->memo, key, result ? (void *)result : TYPE_SCOPE_MISS);
return result;
}
if (f->bindings) {
/* A head the module binds more than once has no one answer. */
char head[CBM_SZ_256];
@@ -508,6 +704,9 @@ const char *cbm_type_scope_resolve(cbm_type_scope_file_t *f, const char *spellin
result = own_binds && !same ? NULL : via_import;
} else if (!bound) {
result = own;
if (!result && (f->open_count > 0 || f->lang->implicit_external)) {
result = type_scope_via_open(f, dotted, head_len);
}
}
cbm_ht_set(f->memo, key, result ? (void *)result : TYPE_SCOPE_MISS);
return result;
@@ -654,6 +853,9 @@ static void type_scope_names(const cbm_type_scope_lang_t *lang, const char *text
for (;;) {
if (i + 1 < n && text[i] == '.' && type_scope_ident_start((unsigned char)text[i + 1])) {
i++;
} else if (lang->colon_paths && i + 2 < n && text[i] == ':' && text[i + 1] == ':' &&
type_scope_ident_start((unsigned char)text[i + 2])) {
i += 2;
} else {
break;
}
@@ -661,6 +863,9 @@ static void type_scope_names(const cbm_type_scope_lang_t *lang, const char *text
i++;
}
}
if (lang->colon_paths && start >= 2 && text[start - 1] == ':' && text[start - 2] == ':') {
start -= 2; /* `::T`, the global scope's: kept whole, it has no identity */
}
bool member = start > 0 && text[start - 1] == '.' && (start < 2 || text[start - 2] != '.');
size_t after = i;
while (after < n && text[after] == ' ') {
@@ -675,6 +880,17 @@ static void type_scope_names(const cbm_type_scope_lang_t *lang, const char *text
/* ── Attaching identities to a definition ─────────────────────────── */
/* True when `label` is one the row's identities may name (a type the engine
* registers). */
static bool type_scope_label_allowed(const cbm_type_scope_lang_t *lang, const char *label) {
for (const char *const *l = lang->labels; label && l && *l; l++) {
if (strcmp(*l, label) == 0) {
return true;
}
}
return false;
}
typedef struct {
cbm_type_scope_file_t *file;
char *buf; /* "spelling=QN|..." being built (heap) */
@@ -722,6 +938,31 @@ static bool type_scope_append(type_scope_build_t *b, const char *s, size_t n) {
return true;
}
/* nested_types / lexical nesting: the answer through the definition's
* enclosing scopes (from file->nest_start outward). *decided: an enclosing
* scope declares the head, so its answer (possibly none) is final; a
* lexical-nesting row resolves through nested scopes only. */
static const char *type_scope_nested(cbm_type_scope_file_t *f, const char *name, size_t len,
bool *decided) {
bool lexical = type_scope_needs_def(f->lang);
*decided = lexical;
size_t dotted_len = len;
const char *dotted = type_scope_dotted(f, name, len, &dotted_len);
size_t head_len = dotted ? type_scope_head_len(dotted, dotted_len) : 0;
if (!dotted || head_len == 0) {
*decided = true; /* `::T`, or no memory: no identity */
return NULL;
}
size_t min_len = lexical ? f->scope_len : f->scope_len + 1;
const char *scope = type_scope_declaring_scope(f, f->nest_start, min_len, dotted, head_len);
if (!scope) {
return NULL;
}
*decided = true;
const char *qn = cbm_arena_sprintf(&f->keys, "%s.%s", scope, dotted);
return qn ? type_scope_target(f, qn) : NULL;
}
static void type_scope_on_name(const char *name, size_t len, void *ud) {
type_scope_build_t *b = (type_scope_build_t *)ud;
if (b->failed || type_scope_seen(b->file, name, len)) {
@@ -730,7 +971,11 @@ static void type_scope_on_name(const char *name, size_t len, void *ud) {
if (b->file->lang->lexical_scopes && type_scope_shadowed(b->file, name, len)) {
return;
}
const char *qn = cbm_type_scope_resolve(b->file, name, len);
bool decided = false;
const char *qn = b->file->nest_memo ? type_scope_nested(b->file, name, len, &decided) : NULL;
if (!decided) {
qn = cbm_type_scope_resolve(b->file, name, len);
}
if (!qn) {
return;
}
@@ -785,6 +1030,18 @@ void cbm_type_scope_attach(cbm_type_scope_file_t *file, CBMArena *arena, CBMLSPD
const cbm_type_scope_lang_t *lang = file->lang;
file->serial++;
file->enclosing_class = NULL;
file->nest_start = NULL;
if (file->nest_memo && def->qualified_name) {
/* A type's own texts are written inside it (nested_types); everything
* else, and every lexical-nesting def, inside its parent. */
bool inside_self =
!type_scope_needs_def(lang) && type_scope_label_allowed(lang, def->label);
const char *dot = strrchr(def->qualified_name, '.');
file->nest_start = inside_self ? def->qualified_name
: dot ? cbm_arena_strndup(&file->keys, def->qualified_name,
(size_t)(dot - def->qualified_name))
: NULL;
}
if (lang->lexical_scopes) {
/* A module whose bindings cannot be listed (a star import, an effect
* binding unknown names), and a def inside a function (it sees locals
@@ -819,3 +1076,53 @@ void cbm_type_scope_attach(cbm_type_scope_file_t *file, CBMArena *arena, CBMLSPD
}
cbm_free(CBM_MEM_CLASS_EXTRACT, b.buf);
}
/* ── The calling file's own identities ───────────────────────────── */
int cbm_type_scope_file_imports(cbm_type_scope_file_t *file, const char *const **out_locals,
const char *const **out_qns) {
if (out_locals) {
*out_locals = NULL;
}
if (out_qns) {
*out_qns = NULL;
}
if (!file) {
return 0;
}
if (!file->fi_built) {
file->fi_built = true;
/* Only an import-map scope has named imports to verify, and a module
* whose bindings cannot be listed has none. */
int n = file->lang->scope_source == CBM_TYPE_SCOPE_MODULE_IMPORTS && !file->closed
? file->scope.imp_count
: 0;
CBMHashTable *done =
n > 0 ? cbm_ht_create_in(CBM_MEM_CLASS_EXTRACT, (uint32_t)(n + 1)) : NULL;
file->fi_locals = done ? cbm_arena_alloc(&file->keys, (size_t)n * sizeof(char *)) : NULL;
file->fi_qns = done ? cbm_arena_alloc(&file->keys, (size_t)n * sizeof(char *)) : NULL;
for (int i = 0; file->fi_locals && file->fi_qns && i < n; i++) {
const char *local = file->scope.imp_keys ? file->scope.imp_keys[i] : NULL;
TYPE_SCOPE_WORK(1);
if (!local || !local[0] || cbm_ht_get(done, local) ||
(file->lang->open_imports && strcmp(local, CBM_TYPE_SCOPE_OPEN_KEY) == 0)) {
continue;
}
cbm_ht_set(done, local, (void *)local);
const char *qn = cbm_type_scope_resolve(file, local, strlen(local));
if (qn) {
file->fi_locals[file->fi_count] = local;
file->fi_qns[file->fi_count] = qn;
file->fi_count++;
}
}
cbm_ht_free(done); /* NULL-safe */
}
if (out_locals) {
*out_locals = (const char *const *)file->fi_locals;
}
if (out_qns) {
*out_qns = (const char *const *)file->fi_qns;
}
return file->fi_count;
}
+59 -1
View File
@@ -47,12 +47,34 @@
#include <stdbool.h>
#include <stddef.h>
/* Where the declarations a spelling can name come from, beyond the
* definition's own enclosing scopes. */
typedef enum {
/* The file's own module, then what its imports bind (the import map). */
CBM_TYPE_SCOPE_MODULE_IMPORTS = 0,
/* The definitions visible through the file's include closure (C, C++):
* the caller's cbm_type_scope_t.visible answers a name; without it no
* name has an identity. */
CBM_TYPE_SCOPE_INCLUDE_CLOSURE,
/* Lexical nesting (Ruby constants): the namespaces of this file that
* enclose the definition, innermost first; a name none of them declares
* has no identity. */
CBM_TYPE_SCOPE_LEXICAL_NESTING,
} cbm_type_scope_source_t;
/* The import-map key of an open import (see open_imports). */
#define CBM_TYPE_SCOPE_OPEN_KEY "*"
/* How one language writes type names, and what its engine consumes. */
typedef struct {
CBMLanguage lang;
cbm_type_scope_source_t scope_source;
/* A name inside quotes is a type name (Python forward references); in
* every other language a quoted run is a literal and is skipped. */
bool quoted_names;
/* `::` joins the parts of a qualified name as `.` does (Rust, C++, Ruby
* `a::B`): the spelling stays as written, its QN joins with `.`. */
bool colon_paths;
/* Labels of the definitions an identity may name: the type kinds the
* engine registers and can look members up on. NULL-terminated. */
const char *const *labels;
@@ -74,7 +96,28 @@ typedef struct {
* a module (a class `Thing` in pkg/a/__init__.py beside pkg/a/Thing.py:
* their members share QNs, so no member answer is that class's). */
bool lexical_scopes;
/* Which CBMLSPDef texts hold raw type spellings for this language. */
/* Open (on-demand) imports (Java and Kotlin `.*`, C# `using N;`): each
* import-map entry under CBM_TYPE_SCOPE_OPEN_KEY names a package whose
* every type is visible. A bare name the own module and the named
* imports leave unbound is `<package>.<name>` in each open package: one
* target across all of them is its identity, two or more are none.
* Without the flag the key is an ordinary local name. */
bool open_imports;
/* The language's implicit external scope (java.lang, kotlin.*, the C#
* global namespace): true when a bare name is declared there. A name the
* own module and the named imports leave unbound and this answers true
* for has no identity (it is the external type, or ambiguous with an open
* import). NULL: no such scope. */
bool (*implicit_external)(const char *name, size_t len);
/* Member types (Java, C#, Kotlin): a bare name written in a definition
* nested in a type names that type's member type of the name first, then
* each enclosing type's, innermost first (`Inner` written inside `Outer`
* is `Outer.Inner`), before the module and the imports. A member type a
* supertype declares is not seen: past an enclosing type with a supertype
* the answer is a possible type, not a proven one. */
bool nested_types;
/* Which CBMLSPDef texts hold raw type spellings for this language. Every
* row states each one. */
bool return_types;
bool param_types;
bool field_types; /* "name:type|name:type" in field_defs */
@@ -106,6 +149,11 @@ typedef struct {
* labels), so a QN that is a module's gets no identity; NULL = not
* checked. */
const struct cbm_gbuf *graph;
/* CBM_TYPE_SCOPE_INCLUDE_CLOSURE only: the one definition named `name`
* (len bytes) the file sees through its include closure, or NULL (none,
* several, or the closure is incomplete). */
const char *(*visible)(void *ud, const char *name, size_t len);
void *visible_ud;
} cbm_type_scope_t;
/* Per-file working state: the import map by key, the file's own scopes, and
@@ -128,6 +176,16 @@ const char *cbm_type_scope_resolve(cbm_type_scope_file_t *file, const char *spel
* allocated in `arena`. */
void cbm_type_scope_attach(cbm_type_scope_file_t *file, CBMArena *arena, CBMLSPDef *def);
/* The calling file's own identity table: each local name the file's named
* imports bind, with the type identity it resolves to in the file's scope
* (one answer of a label the engine registers, as for a declared type), in
* import-map order, each local once. An engine resolving a bare name at a
* call site reads it instead of an import's unverified target. The arrays
* and strings are owned by the file state (valid until file_end). Returns
* the count (0: none). */
int cbm_type_scope_file_imports(cbm_type_scope_file_t *file, const char *const **out_locals,
const char *const **out_qns);
#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS
/* Work counter for the growth tests: one per byte of type text scanned, per
* hash probe and per enclosing-scope step. Test builds only. */
+449
View File
@@ -455,6 +455,445 @@ TEST(type_scope_engine_table_rows) {
PASS();
}
/* ── Row fields for the further engines (hand-made rows) ──────────── */
static const char *const ts_row_labels[] = {"Class", "Interface", NULL};
/* A row of an import-map language whose engine does not qualify its own
* module (every identity is stored), with the given extras. */
static cbm_type_scope_lang_t ts_row(void) {
cbm_type_scope_lang_t row = {.lang = CBM_LANG_JAVA,
.scope_source = CBM_TYPE_SCOPE_MODULE_IMPORTS,
.labels = ts_row_labels,
.return_types = true,
.param_types = true,
.field_types = true,
.embedded_types = true};
return row;
}
/* The type_scope of one def (qn, label, return text) under `row` in module
* p.app, with the scope's extras (visible) from `extra` when given. */
static const char *ts_row_attach(ts_unit_t *u, const cbm_type_scope_lang_t *row, const char *def_qn,
const char *label, const char *ret, const char *const *keys,
const char *const *vals, int n, const cbm_type_scope_t *extra) {
cbm_type_scope_t scope = {
.reg = u->reg, .module_qn = "p.app", .imp_keys = keys, .imp_vals = vals, .imp_count = n};
if (extra) {
scope.visible = extra->visible;
scope.visible_ud = extra->visible_ud;
}
cbm_type_scope_file_t *f = cbm_type_scope_file_begin(row, &scope);
if (!f) {
return "<no file state>";
}
CBMLSPDef def = {0};
def.qualified_name = def_qn;
def.short_name = "f";
def.label = label;
def.return_types = ret;
cbm_type_scope_attach(f, &u->arena, &def);
cbm_type_scope_file_end(f);
return def.type_scope;
}
/* open_imports: a name the own module and the named imports leave unbound is
* the one type of that name across the open packages, or nothing. */
TEST(type_scope_open_imports_one_answer_or_none) {
ts_unit_t u;
ts_unit_begin(&u);
cbm_registry_add(u.reg, "Widget", "p.x.Widget", "Class", false);
cbm_registry_add(u.reg, "Widget", "p.y.Widget", "Class", false);
cbm_registry_add(u.reg, "Gadget", "p.y.Gadget", "Class", false);
cbm_registry_add(u.reg, "Local", "p.x.Local", "Class", false);
cbm_registry_add(u.reg, "Thing", "p.app.Thing", "Function", false);
cbm_registry_add(u.reg, "Thing", "p.x.Thing", "Class", false);
cbm_type_scope_lang_t row = ts_row();
row.open_imports = true;
const char *keys[] = {"*", "*", "*"};
const char *vals[] = {"p.x", "p.y", "p.x"};
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.f", "Function", "Gadget", keys, vals, 3, NULL),
"Gadget=p.y.Gadget");
/* Two open packages declare Widget: none. */
ASSERT_NULL(ts_row_attach(&u, &row, "p.app.f", "Function", "Widget", keys, vals, 3, NULL));
/* The own module shadows the open packages: its Local, its Thing (a
* function, no type: none). */
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.f", "Function", "Local", keys, vals, 3, NULL),
"Local=p.app.Local");
ASSERT_NULL(ts_row_attach(&u, &row, "p.app.f", "Function", "Thing", keys, vals, 3, NULL));
/* A named import shadows them too. */
const char *nkeys[] = {"*", "*", "Widget"};
const char *nvals[] = {"p.x", "p.y", "p.x.Widget"};
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.f", "Function", "Widget", nkeys, nvals, 3, NULL),
"Widget=p.x.Widget");
/* Without the flag "*" is an ordinary local name: Gadget is unbound. */
row.open_imports = false;
ASSERT_NULL(ts_row_attach(&u, &row, "p.app.f", "Function", "Gadget", keys, vals, 3, NULL));
ts_unit_end(&u);
PASS();
}
static bool ts_implicit_string(const char *name, size_t len) {
return len == 6 && memcmp(name, "String", 6) == 0;
}
/* implicit_external: a name the language's implicit external scope declares
* (java.lang.String) is no project type of an open package. */
TEST(type_scope_implicit_external_scope_gets_none) {
ts_unit_t u;
ts_unit_begin(&u);
cbm_registry_add(u.reg, "String", "p.y.String", "Class", false);
cbm_registry_add(u.reg, "Gadget", "p.y.Gadget", "Class", false);
cbm_type_scope_lang_t row = ts_row();
row.open_imports = true;
const char *keys[] = {"*"};
const char *vals[] = {"p.y"};
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.f", "Function", "String", keys, vals, 1, NULL),
"String=p.y.String");
row.implicit_external = ts_implicit_string;
ASSERT_NULL(ts_row_attach(&u, &row, "p.app.f", "Function", "String", keys, vals, 1, NULL));
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.f", "Function", "Gadget", keys, vals, 1, NULL),
"Gadget=p.y.Gadget");
/* The own module's String still shadows the implicit one. */
cbm_registry_add(u.reg, "String", "p.app.String", "Class", false);
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.f", "Function", "String", keys, vals, 1, NULL),
"String=p.app.String");
ts_unit_end(&u);
PASS();
}
/* nested_types: `Inner` written inside `Outer` is `Outer.Inner`, innermost
* enclosing type first, before the module. */
TEST(type_scope_nested_types_innermost_first) {
ts_unit_t u;
ts_unit_begin(&u);
cbm_registry_add(u.reg, "Outer", "p.app.Outer", "Class", false);
cbm_registry_add(u.reg, "Inner", "p.app.Outer.Inner", "Class", false);
cbm_registry_add(u.reg, "Leaf", "p.app.Outer.Inner.Leaf", "Class", false);
cbm_registry_add(u.reg, "Deep", "p.app.Outer.Deep", "Class", false);
cbm_registry_add(u.reg, "Inner", "p.app.Outer.Deep.Inner", "Class", false);
cbm_registry_add(u.reg, "Inner", "p.app.Inner", "Class", false);
cbm_registry_add(u.reg, "Gizmo", "p.app.Outer.Gizmo", "Method", false);
cbm_registry_add(u.reg, "Gizmo", "p.app.Gizmo", "Class", false);
cbm_type_scope_lang_t row = ts_row();
row.nested_types = true;
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.Outer.m", "Method", "Inner", NULL, NULL, 0, NULL),
"Inner=p.app.Outer.Inner");
ASSERT_STR_EQ(
ts_row_attach(&u, &row, "p.app.Outer.Deep.n", "Method", "Inner", NULL, NULL, 0, NULL),
"Inner=p.app.Outer.Deep.Inner");
/* A type's own texts are written inside it. */
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.Outer", "Class", "Inner", NULL, NULL, 0, NULL),
"Inner=p.app.Outer.Inner");
ASSERT_STR_EQ(
ts_row_attach(&u, &row, "p.app.Outer.m", "Method", "Inner.Leaf", NULL, NULL, 0, NULL),
"Inner.Leaf=p.app.Outer.Inner.Leaf");
/* At module level, the module's. */
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.f", "Function", "Inner", NULL, NULL, 0, NULL),
"Inner=p.app.Inner");
/* The enclosing type declares Gizmo as a method: the module's Gizmo is
* hidden, and a method is no type: nothing. */
ASSERT_NULL(ts_row_attach(&u, &row, "p.app.Outer.m", "Method", "Gizmo", NULL, NULL, 0, NULL));
/* Without the flag the module answers. */
row.nested_types = false;
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.Outer.m", "Method", "Inner", NULL, NULL, 0, NULL),
"Inner=p.app.Inner");
ts_unit_end(&u);
PASS();
}
static const char *ts_visible_widget(void *ud, const char *name, size_t len) {
(void)ud;
return len == 6 && memcmp(name, "Widget", 6) == 0 ? "p.h.Widget" : NULL;
}
/* scope_source INCLUDE_CLOSURE: the caller's closure answers the head. */
TEST(type_scope_include_closure_source) {
ts_unit_t u;
ts_unit_begin(&u);
cbm_registry_add(u.reg, "Widget", "p.h.Widget", "Class", false);
cbm_registry_add(u.reg, "Part", "p.h.Widget.Part", "Class", false);
cbm_registry_add(u.reg, "Gadget", "p.app.Gadget", "Class", false);
cbm_type_scope_lang_t row = ts_row();
row.scope_source = CBM_TYPE_SCOPE_INCLUDE_CLOSURE;
cbm_type_scope_t extra = {.visible = ts_visible_widget};
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.f", "Function", "Widget *", NULL, NULL, 0, &extra),
"Widget=p.h.Widget");
ASSERT_STR_EQ(
ts_row_attach(&u, &row, "p.app.f", "Function", "Widget.Part", NULL, NULL, 0, &extra),
"Widget.Part=p.h.Widget.Part");
/* Only the closure answers: the own module's Gadget is not visible by
* module, and without a closure nothing is. */
ASSERT_NULL(ts_row_attach(&u, &row, "p.app.f", "Function", "Gadget", NULL, NULL, 0, &extra));
ASSERT_NULL(ts_row_attach(&u, &row, "p.app.f", "Function", "Widget", NULL, NULL, 0, NULL));
ts_unit_end(&u);
PASS();
}
/* scope_source LEXICAL_NESTING with `::` paths (Ruby constants): the
* namespaces enclosing the def, innermost first, then the file's top level;
* `::X` and a name nothing nests get none. */
TEST(type_scope_lexical_nesting_source) {
ts_unit_t u;
ts_unit_begin(&u);
cbm_registry_add(u.reg, "A", "p.app.A", "Class", false);
cbm_registry_add(u.reg, "B", "p.app.A.B", "Class", false);
cbm_registry_add(u.reg, "X", "p.app.A.X", "Class", false);
cbm_registry_add(u.reg, "X", "p.app.X", "Class", false);
cbm_registry_add(u.reg, "Y", "p.app.A.B.Y", "Class", false);
cbm_type_scope_lang_t row = ts_row();
row.scope_source = CBM_TYPE_SCOPE_LEXICAL_NESTING;
row.colon_paths = true;
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.A.B.m", "Method", "X", NULL, NULL, 0, NULL),
"X=p.app.A.X");
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.A.B.m", "Method", "Y", NULL, NULL, 0, NULL),
"Y=p.app.A.B.Y");
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.A.B.m", "Method", "A::X", NULL, NULL, 0, NULL),
"A::X=p.app.A.X");
ASSERT_STR_EQ(ts_row_attach(&u, &row, "p.app.f", "Function", "X", NULL, NULL, 0, NULL),
"X=p.app.X");
ASSERT_NULL(ts_row_attach(&u, &row, "p.app.A.B.m", "Method", "::X", NULL, NULL, 0, NULL));
ASSERT_NULL(ts_row_attach(&u, &row, "p.app.A.B.m", "Method", "Z", NULL, NULL, 0, NULL));
/* Outside a definition (no nesting) the row answers nothing. */
ts_facts_t empty;
cbm_type_scope_t scope = {
.reg = u.reg, .module_qn = "p.app", .module_facts = ts_facts(&empty, NULL, 0)};
cbm_type_scope_file_t *f = cbm_type_scope_file_begin(&row, &scope);
ASSERT_NOT_NULL(f);
ASSERT_NULL(cbm_type_scope_resolve(f, "X", 1));
cbm_type_scope_file_end(f);
ts_unit_end(&u);
PASS();
}
/* colon_paths: `ns::T` is one spelling, its QN joined with `.`; without the
* flag `ns` and `T` are two names. */
TEST(type_scope_colon_paths) {
ts_unit_t u;
ts_unit_begin(&u);
cbm_registry_add(u.reg, "T", "p.app.ns.T", "Class", false);
cbm_registry_add(u.reg, "vector", "p.app.std.vector", "Class", false);
cbm_type_scope_lang_t row = ts_row();
row.colon_paths = true;
ASSERT_STR_EQ(
ts_row_attach(&u, &row, "p.app.f", "Function", "std::vector<ns::T>", NULL, NULL, 0, NULL),
"std::vector=p.app.std.vector|ns::T=p.app.ns.T");
row.colon_paths = false;
ASSERT_NULL(ts_row_attach(&u, &row, "p.app.f", "Function", "ns::T", NULL, NULL, 0, NULL));
ts_unit_end(&u);
PASS();
}
/* The calling file's own table: each local its named imports bind, with the
* type identity it resolves to; Go binds packages (none); the open key and
* a non-type are left out. */
TEST(type_scope_calling_file_imports) {
ts_unit_t u;
ts_unit_begin(&u);
ts_facts_t facts;
const char *keys[] = {"Store", "make", "Store", "Other", "Local"};
const char *vals[] = {"p.a.Store", "p.c.make", "p.a.Store", "p.z.Other", "p.app.Local"};
const char *bound[] = {"Store", "make", "Other", "Local"};
cbm_type_scope_t scope = {.reg = u.reg,
.module_qn = "p.app",
.imp_keys = keys,
.imp_vals = vals,
.imp_count = 5,
.module_facts = ts_facts(&facts, bound, 4)};
cbm_type_scope_file_t *f =
cbm_type_scope_file_begin(cbm_type_scope_lang(CBM_LANG_PYTHON), &scope);
ASSERT_NOT_NULL(f);
const char *const *locals = NULL;
const char *const *qns = NULL;
ASSERT_EQ(cbm_type_scope_file_imports(f, &locals, &qns), 2);
ASSERT_STR_EQ(locals[0], "Store");
ASSERT_STR_EQ(qns[0], "p.a.Store");
ASSERT_STR_EQ(locals[1], "Local");
ASSERT_STR_EQ(qns[1], "p.app.Local");
/* Built once: the same arrays again. */
const char *const *again = NULL;
ASSERT_EQ(cbm_type_scope_file_imports(f, &again, NULL), 2);
ASSERT(again == locals);
cbm_type_scope_file_end(f);
/* A module whose bindings cannot be listed (a star import) has none. */
facts.items[4] = (CBMPyNamespaceFact){.kind = CBM_PY_NS_IMPORT_STAR};
facts.facts.count = 5;
f = cbm_type_scope_file_begin(cbm_type_scope_lang(CBM_LANG_PYTHON), &scope);
ASSERT_NOT_NULL(f);
ASSERT_EQ(cbm_type_scope_file_imports(f, &locals, &qns), 0);
cbm_type_scope_file_end(f);
const char *gkeys[] = {"a"};
const char *gvals[] = {"p.a"};
cbm_type_scope_t go = {
.reg = u.reg, .module_qn = "p.app", .imp_keys = gkeys, .imp_vals = gvals, .imp_count = 1};
f = cbm_type_scope_file_begin(cbm_type_scope_lang(CBM_LANG_GO), &go);
ASSERT_NOT_NULL(f);
ASSERT_EQ(cbm_type_scope_file_imports(f, &locals, &qns), 0);
cbm_type_scope_file_end(f);
ASSERT_EQ(cbm_type_scope_file_imports(NULL, &locals, &qns), 0);
ASSERT_NULL(locals);
/* An open row leaves its open key out. */
cbm_type_scope_lang_t row = ts_row();
row.open_imports = true;
const char *okeys[] = {"*", "Store"};
const char *ovals[] = {"p.a", "p.a.Store"};
cbm_type_scope_t open = {
.reg = u.reg, .module_qn = "p.app", .imp_keys = okeys, .imp_vals = ovals, .imp_count = 2};
f = cbm_type_scope_file_begin(&row, &open);
ASSERT_NOT_NULL(f);
ASSERT_EQ(cbm_type_scope_file_imports(f, &locals, &qns), 1);
ASSERT_STR_EQ(locals[0], "Store");
cbm_type_scope_file_end(f);
ts_unit_end(&u);
PASS();
}
/* A walk reads its calling file's table from the same scope the collector
* attaches with: none without a context or for an engine that reads no
* identities. */
TEST(type_scope_calling_file_scope_per_engine) {
ts_unit_t u;
ts_unit_begin(&u);
ts_facts_t facts;
const char *keys[] = {"Store"};
const char *vals[] = {"p.a.Store"};
CBMFileResult *fr = calloc(1, sizeof(*fr));
ASSERT_NOT_NULL(fr);
fr->py_namespace = *ts_facts(&facts, keys, 1);
cbm_pipeline_ctx_t *ctx = calloc(1, sizeof(*ctx));
ASSERT_NOT_NULL(ctx);
ctx->registry = u.reg;
cbm_type_scope_file_t *f = cbm_pxc_file_scope(ctx, CBM_LANG_PYTHON, fr, "p.app", keys, vals, 1);
ASSERT_NOT_NULL(f);
const char *const *locals = NULL;
const char *const *qns = NULL;
ASSERT_EQ(cbm_type_scope_file_imports(f, &locals, &qns), 1);
ASSERT_STR_EQ(qns[0], "p.a.Store");
cbm_type_scope_file_end(f);
ASSERT_NULL(cbm_pxc_file_scope(NULL, CBM_LANG_PYTHON, fr, "p.app", keys, vals, 1));
ASSERT_NULL(cbm_pxc_file_scope(ctx, CBM_LANG_CPP, fr, "p.app", keys, vals, 1));
ASSERT_NULL(cbm_pxc_file_scope(ctx, CBM_LANG_JAVA, fr, "p.app", keys, vals, 1));
free(ctx);
free(fr);
ts_unit_end(&u);
PASS();
}
/* Counted work of the step for `defs` methods four types deep, each writing
* `names` member-type names (nested_types), or the same names through
* `open` open packages (open_imports). */
static uint64_t ts_row_work(int defs, int names, int open, bool nested) {
ts_unit_t u;
ts_unit_begin(&u);
cbm_registry_add(u.reg, "N0", "p.app.N0", "Class", false);
cbm_registry_add(u.reg, "N1", "p.app.N0.N1", "Class", false);
cbm_registry_add(u.reg, "N2", "p.app.N0.N1.N2", "Class", false);
cbm_registry_add(u.reg, "N3", "p.app.N0.N1.N2.N3", "Class", false);
char buf[64];
size_t text_cap = (size_t)names * 16 + 1;
char *text = calloc(1, text_cap);
const char *keys[16];
const char *vals[16];
char owned[16][16];
open = open < 16 ? open : 16;
for (int i = 0; i < names; i++) {
snprintf(buf, sizeof(buf), nested ? "p.app.N0.M%d" : "p.o0.M%d", i);
cbm_registry_add(u.reg, "M", buf, "Class", false);
size_t at = strlen(text);
snprintf(text + at, text_cap - at, "%sM%d", i ? "|" : "", i);
}
for (int i = 0; i < open; i++) {
snprintf(owned[i], sizeof(owned[i]), "p.o%d", i);
keys[i] = "*";
vals[i] = owned[i];
}
cbm_type_scope_lang_t row = ts_row();
row.nested_types = nested;
row.open_imports = !nested;
cbm_type_scope_t scope = {
.reg = u.reg, .module_qn = "p.app", .imp_keys = keys, .imp_vals = vals, .imp_count = open};
cbm_type_scope_file_t *f = cbm_type_scope_file_begin(&row, &scope);
uint64_t before = cbm_type_scope_test_work();
for (int d = 0; f && d < defs; d++) {
snprintf(buf, sizeof(buf), "p.app.N0.N1.N2.N3.m%d", d);
CBMLSPDef def = {.qualified_name = buf, .short_name = "m", .label = "Method"};
def.return_types = text;
cbm_type_scope_attach(f, &u.arena, &def);
}
uint64_t work = cbm_type_scope_test_work() - before;
cbm_type_scope_file_end(f);
free(text);
ts_unit_end(&u);
return work;
}
TEST(type_scope_nested_and_open_work_grow_linearly) {
struct {
const char *what;
uint64_t small;
uint64_t large;
} cases[] = {
{"nested, defs", ts_row_work(500, 40, 0, true), ts_row_work(1000, 40, 0, true)},
{"nested, names", ts_row_work(50, 400, 0, true), ts_row_work(50, 800, 0, true)},
{"open, defs", ts_row_work(500, 40, 8, false), ts_row_work(1000, 40, 8, false)},
{"open, names", ts_row_work(50, 400, 8, false), ts_row_work(50, 800, 8, false)},
};
for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
double ratio = (double)cases[i].large / (double)(cases[i].small ? cases[i].small : 1);
printf(" %s: %llu -> %llu (x%.2f)\n", cases[i].what, (unsigned long long)cases[i].small,
(unsigned long long)cases[i].large, ratio);
ASSERT_GT(cases[i].small, 0);
ASSERT(ratio > 1.7 && ratio < 2.3);
}
PASS();
}
/* The fold's rows keep each engine's present case: Go and C fold their
* structs' fields in declaration order; CUDA files fold none. */
static const char *ts_folded_fields(CBMLanguage lang, const char *rel, const char *src,
const char *owner_label, char *out, size_t cap) {
out[0] = '\0';
CBMFileResult *r = cbm_extract_file(src, (int)strlen(src), lang, "p", rel, 0, NULL, NULL);
if (!r) {
return "<no result>";
}
CBMFileResult *cache[] = {r};
char rel_path[32];
snprintf(rel_path, sizeof(rel_path), "%s", rel);
cbm_file_info_t files[] = {{.rel_path = rel_path, .language = lang}};
char *modules[1] = {NULL};
int count = 0;
CBMArena arena;
cbm_arena_init(&arena);
CBMLSPDef *defs =
cbm_pxc_collect_all_defs(NULL, &arena, cache, files, 1, "p", modules, &count, NULL);
for (int i = 0; defs && i < count; i++) {
if (defs[i].label && strcmp(defs[i].label, owner_label) == 0) {
snprintf(out, cap, "%s", defs[i].field_defs ? defs[i].field_defs : "(none)");
break;
}
}
free(defs);
free(modules[0]);
cbm_arena_destroy(&arena);
cbm_free_result(r);
return out;
}
TEST(type_scope_field_fold_rows) {
char got[128];
ASSERT_STR_EQ(ts_folded_fields(CBM_LANG_GO, "a.go",
"package p\ntype T struct {\n\tA int\n\tB *T\n}\n", "Struct",
got, sizeof(got)),
"A:int|B:*T");
const char *c_src = "struct point { int x; double y; };\n";
ASSERT_STR_EQ(ts_folded_fields(CBM_LANG_C, "a.c", c_src, "Class", got, sizeof(got)),
"x:int|y:double");
ASSERT_STR_EQ(ts_folded_fields(CBM_LANG_CUDA, "a.cu", c_src, "Class", got, sizeof(got)),
"(none)");
PASS();
}
/* ── Growth: counted work at two sizes ─────────────────────────────── */
static bool ts_linear(uint64_t small, uint64_t large) {
@@ -2479,6 +2918,16 @@ SUITE(type_scope) {
RUN_TEST(type_scope_python_type_parameters);
RUN_TEST(type_scope_python_unlisted_bindings_get_none);
RUN_TEST(type_scope_engine_table_rows);
RUN_TEST(type_scope_open_imports_one_answer_or_none);
RUN_TEST(type_scope_implicit_external_scope_gets_none);
RUN_TEST(type_scope_nested_types_innermost_first);
RUN_TEST(type_scope_include_closure_source);
RUN_TEST(type_scope_lexical_nesting_source);
RUN_TEST(type_scope_colon_paths);
RUN_TEST(type_scope_calling_file_imports);
RUN_TEST(type_scope_calling_file_scope_per_engine);
RUN_TEST(type_scope_nested_and_open_work_grow_linearly);
RUN_TEST(type_scope_field_fold_rows);
RUN_TEST(type_scope_step_work_grows_linearly);
RUN_TEST(type_scope_lookup_work_grows_linearly);
RUN_TEST(type_scope_reach_work_grows_linearly);