Skip to content

Commit 6e0c59f

Browse files
authored
Merge pull request #2867 from DeusData/fix/cs-lsp-nested-types-global-ns
fix(cs-lsp): resolve nested types through type qualifiers, bind global-namespace types
2 parents 29aa748 + 21c253f commit 6e0c59f

6 files changed

Lines changed: 599 additions & 28 deletions

File tree

‎internal/cbm/cbm.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,9 @@ typedef struct {
289289
* qualified_name (base QN = the first qn_sig_off bytes); 0 = no suffix.
290290
* Always 0 until a language enables its callable_identity mode. */
291291
uint32_t qn_sig_off;
292-
/* C# only: declared namespace of a TOP-LEVEL type (NULL for nested types,
293-
* the global namespace and every other language). Per type, because the
292+
/* C# only: declared namespace of a TOP-LEVEL type, "" for the global
293+
* namespace (proven only by a clean parse), NULL for nested types, an
294+
* unknown namespace and every other language. Per type, because the
294295
* file-level namespace_name records only a file's first namespace. */
295296
const char *decl_namespace;
296297
CBMTestDefinitionRole test_role; /* NONE preserves legacy helper/test-file semantics */

‎internal/cbm/extract_defs.c‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8340,9 +8340,15 @@ static const char *cs_namespace_decl_name(CBMArena *a, TSNode ns, const char *so
83408340
/* Declared namespace of a C# type declaration: every enclosing namespace
83418341
* block (outer to inner) under an optional file-scoped namespace, which the
83428342
* grammar may attach either as an ancestor or as a preceding sibling of the
8343-
* compilation unit's members. NULL = global namespace. The file-level
8344-
* namespace_name keeps only the FIRST namespace of a file, which mislabels
8345-
* every type of a multi-namespace file (#2120). */
8343+
* compilation unit's members. "" = the global namespace, NULL = unknown. The
8344+
* file-level namespace_name keeps only the FIRST namespace of a file, which
8345+
* mislabels every type of a multi-namespace file (#2120).
8346+
*
8347+
* "No namespace found" proves the global namespace only for a clean parse:
8348+
* error recovery can dissolve a namespace declaration around a construct the
8349+
* grammar does not know (a `ref partial struct` with members does it), leaving
8350+
* the types after it parented straight under the compilation unit. Calling
8351+
* those global would let them hide every imported type of the same name. */
83468352
static const char *cs_type_decl_namespace(CBMArena *a, TSNode node, const char *source) {
83478353
const char *acc = NULL;
83488354
bool file_scoped = false;
@@ -8376,7 +8382,10 @@ static const char *cs_type_decl_namespace(CBMArena *a, TSNode node, const char *
83768382
}
83778383
}
83788384
}
8379-
return acc;
8385+
if (acc) {
8386+
return acc;
8387+
}
8388+
return ts_node_has_error(top) ? NULL : "";
83808389
}
83818390

83828391
static void extract_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec) {

‎internal/cbm/lsp/cs_lsp.c‎

Lines changed: 95 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
#define CS_USING_INITIAL_CAP 16
5252
#define CS_NAMESPACE_INITIAL_CAP 8
5353
#define CS_LSP_PARENT_WALK_MAX 32
54+
/* Nested-type qualifier walk depth: one level per type nesting step of `A.B.C`
55+
* (a namespace-qualified top-level type binds without recursing), so 8 covers
56+
* real nesting while a cyclic alias such as `using A = A.B;` stops after 8. */
57+
#define CS_QUALIFIER_MAX_DEPTH 8
5458

5559
extern const TSLanguage *tree_sitter_c_sharp(void);
5660

@@ -257,11 +261,15 @@ void cs_lsp_init(CSLSPContext *ctx, CBMArena *arena, const char *source, int sou
257261

258262
/* ── using management ───────────────────────────────────────────── */
259263

260-
void cs_lsp_add_using(CSLSPContext *ctx, CBMCSUsingKind kind, const char *local_name,
261-
const char *target_qn, bool is_global) {
264+
/* Append a using directive; `in_namespace` marks one declared inside a
265+
* namespace body (CBMCSUsing.in_namespace). */
266+
static void cs_add_using_scoped(CSLSPContext *ctx, CBMCSUsingKind kind, const char *local_name,
267+
const char *target_qn, bool is_global, bool in_namespace) {
262268
if (!target_qn)
263269
return;
264-
/* Dedupe by (kind, local_name, target). */
270+
/* Dedupe by (kind, local_name, target). The cross-file entry pre-loads
271+
* the pipeline's import list without placement; the AST walk then finds
272+
* the same directive and is the authority on where it was declared. */
265273
for (int i = 0; i < ctx->using_count; i++) {
266274
CBMCSUsing *u = &ctx->usings[i];
267275
if (u->kind != kind)
@@ -272,6 +280,7 @@ void cs_lsp_add_using(CSLSPContext *ctx, CBMCSUsingKind kind, const char *local_
272280
const char *b = u->local_name ? u->local_name : "";
273281
if (strcmp(a, b) != 0)
274282
continue;
283+
u->in_namespace = u->in_namespace || in_namespace;
275284
return;
276285
}
277286
if (ctx->using_count >= ctx->using_cap) {
@@ -289,6 +298,12 @@ void cs_lsp_add_using(CSLSPContext *ctx, CBMCSUsingKind kind, const char *local_
289298
slot->local_name = local_name ? cbm_arena_strdup(ctx->arena, local_name) : "";
290299
slot->target_qn = cbm_arena_strdup(ctx->arena, target_qn);
291300
slot->is_global = is_global;
301+
slot->in_namespace = in_namespace;
302+
}
303+
304+
void cs_lsp_add_using(CSLSPContext *ctx, CBMCSUsingKind kind, const char *local_name,
305+
const char *target_qn, bool is_global) {
306+
cs_add_using_scoped(ctx, kind, local_name, target_qn, is_global, false);
292307
}
293308

294309
/* ── namespace stack ────────────────────────────────────────────── */
@@ -358,12 +373,29 @@ static const char *cs_try_type_qn(CSLSPContext *ctx, const char *qn) {
358373
return NULL;
359374
}
360375

376+
/* Append the file's using-namespace targets declared inside (`in_namespace`)
377+
* or outside a namespace body. */
378+
static int cs_append_using_namespaces(const CSLSPContext *ctx, bool in_namespace, const char **out,
379+
int n, int cap) {
380+
for (int i = 0; i < ctx->using_count && n < cap; i++) {
381+
const CBMCSUsing *u = &ctx->usings[i];
382+
if (u->kind == CBM_CS_USING_NAMESPACE && u->in_namespace == in_namespace && u->target_qn &&
383+
u->target_qn[0])
384+
out[n++] = u->target_qn;
385+
}
386+
return n;
387+
}
388+
361389
/* Namespaces through which `qualifier` (the part of a type reference before
362390
* its simple name, NULL for a simple name) can reach a top-level type, in C#
363391
* lookup order: each enclosing namespace innermost outward, then the global
364-
* namespace (qualified references only) or the file's `using` namespaces
365-
* (simple names only -- a using-namespace directive does not import nested
366-
* namespaces). Returns the count written to `out` (at most `cap`). */
392+
* namespace (qualified references only) or, for a simple name, the usings
393+
* of the enclosing namespace bodies, the global namespace "" and last the
394+
* compilation-unit usings (a using-namespace directive does not import nested
395+
* namespaces). C# consults the members of the global namespace BEFORE the
396+
* compilation unit's usings: a top-level type declared without a namespace
397+
* hides an imported type of the same name. Returns the count written to
398+
* `out` (at most `cap`). */
367399
static int cs_visible_namespaces(CSLSPContext *ctx, const char *qualifier, const char **out,
368400
int cap) {
369401
int n = 0;
@@ -378,12 +410,13 @@ static int cs_visible_namespaces(CSLSPContext *ctx, const char *qualifier, const
378410
out[n++] = qualifier;
379411
return n;
380412
}
381-
for (int i = 0; i < ctx->using_count && n < cap; i++) {
382-
const CBMCSUsing *u = &ctx->usings[i];
383-
if (u->kind == CBM_CS_USING_NAMESPACE && u->target_qn && u->target_qn[0])
384-
out[n++] = u->target_qn;
385-
}
386-
return n;
413+
n = cs_append_using_namespaces(ctx, true, out, n, cap);
414+
/* The global namespace (#2120 residual: such types used to be invisible
415+
* to step 8b) ranks after every enclosing namespace and namespace-body
416+
* using, and before the compilation-unit usings. */
417+
if (n < cap)
418+
out[n++] = "";
419+
return cs_append_using_namespaces(ctx, false, out, n, cap);
387420
}
388421

389422
/* Bind a (possibly namespace-qualified) type reference to the project type
@@ -421,6 +454,24 @@ static const char *cs_resolve_by_declared_namespace(CSLSPContext *ctx, const cha
421454
}
422455
}
423456
}
457+
/* No namespace-qualified hit: a qualified `A.B` may name a NESTED type —
458+
* A a type in scope (resolved recursively, one segment shorter), B a
459+
* member type of it. Nested QNs are the parent QN plus the simple name,
460+
* which step 8b's namespace scan can never produce because nested types
461+
* carry no namespace_qn (#2120 residual: `new Command.Result(...)`,
462+
* `new Grain.Handler(dep).Handle(...)`). The walk is depth-bounded: an
463+
* alias whose target names the alias itself (`using A = A.B;`, or a
464+
* mutual pair) re-enters this path through step 8 with no end. */
465+
if (!best && qualifier && ctx->qualifier_depth < CS_QUALIFIER_MAX_DEPTH) {
466+
ctx->qualifier_depth++;
467+
const char *qtype = cs_resolve_type_name(ctx, qualifier);
468+
ctx->qualifier_depth--;
469+
if (qtype) {
470+
const char *nested = cbm_arena_sprintf(ctx->arena, "%s.%s", qtype, simple);
471+
if (cs_lookup_type_qn(ctx, nested))
472+
return nested;
473+
}
474+
}
424475
return best ? best->qualified_name : NULL;
425476
}
426477

@@ -610,10 +661,14 @@ const char *cs_resolve_type_name(CSLSPContext *ctx, const char *raw) {
610661
}
611662

612663
/* Other top-level declarations of the type `t` names: same simple name, same
613-
* declared namespace, different entry. Registration order; at most `cap`. */
664+
* declared namespace, different entry. Registration order; at most `cap`.
665+
* A type in the global namespace ("") has no twins: without a namespace the
666+
* same name across a repository is a different type per assembly (every test
667+
* project's own `Program`), so pooling them would bind a member of one to an
668+
* unrelated other, ahead of the type's own base classes. */
614669
static int cs_namespace_twins(CSLSPContext *ctx, const CBMRegisteredType *t,
615670
const CBMRegisteredType **out, int cap) {
616-
if (!ctx->registry || !t || !t->namespace_qn || !t->short_name)
671+
if (!ctx->registry || !t || !t->namespace_qn || !t->namespace_qn[0] || !t->short_name)
617672
return 0;
618673
int n = 0;
619674
CBMTypeShortIter it;
@@ -2797,12 +2852,19 @@ static void cs_process_type_decl(CSLSPContext *ctx, TSNode node) {
27972852

27982853
static void cs_collect_imports(CSLSPContext *ctx, TSNode root) {
27992854
/* Walk the entire tree once at top — using directives can appear at file
2800-
* scope or inside namespace blocks. */
2801-
TSNode stack[256];
2855+
* scope or inside namespace blocks. `in_ns` records per stacked node
2856+
* whether it sits inside a namespace declaration: a block namespace's
2857+
* body, or the compilation-unit siblings after a file-scoped namespace
2858+
* (the grammar may attach those either way). */
2859+
enum { CS_IMPORT_WALK_MAX = 256 };
2860+
TSNode stack[CS_IMPORT_WALK_MAX];
2861+
bool in_ns[CS_IMPORT_WALK_MAX];
28022862
int top = 0;
2863+
in_ns[top] = false;
28032864
stack[top++] = root;
28042865
while (top > 0) {
28052866
TSNode n = stack[--top];
2867+
bool n_in_ns = in_ns[top];
28062868
if (ts_node_is_null(n))
28072869
continue;
28082870
const char *k = ts_node_type(n);
@@ -2886,20 +2948,23 @@ static void cs_collect_imports(CSLSPContext *ctx, TSNode root) {
28862948
}
28872949
if (target) {
28882950
if (is_alias) {
2889-
cs_lsp_add_using(ctx, CBM_CS_USING_ALIAS, alias_name ? alias_name : "", target,
2890-
is_global);
2951+
cs_add_using_scoped(ctx, CBM_CS_USING_ALIAS, alias_name ? alias_name : "",
2952+
target, is_global, n_in_ns);
28912953
} else if (is_static) {
2892-
cs_lsp_add_using(ctx, CBM_CS_USING_STATIC, "", target, is_global);
2954+
cs_add_using_scoped(ctx, CBM_CS_USING_STATIC, "", target, is_global, n_in_ns);
28932955
} else {
2894-
cs_lsp_add_using(ctx, CBM_CS_USING_NAMESPACE, "", target, is_global);
2956+
cs_add_using_scoped(ctx, CBM_CS_USING_NAMESPACE, "", target, is_global,
2957+
n_in_ns);
28952958
}
28962959
}
28972960
}
28982961
/* Push children for traversal. We don't recurse into method bodies
28992962
* (using directives can't appear there), but namespace bodies may
29002963
* contain more usings. */
2964+
bool child_in_ns = n_in_ns || strcmp(k, "namespace_declaration") == 0 ||
2965+
strcmp(k, "file_scoped_namespace_declaration") == 0;
29012966
uint32_t cnc = ts_node_child_count(n);
2902-
for (uint32_t i = 0; i < cnc && top < 256; i++) {
2967+
for (uint32_t i = 0; i < cnc && top < CS_IMPORT_WALK_MAX; i++) {
29032968
TSNode c = ts_node_child(n, i);
29042969
if (ts_node_is_null(c))
29052970
continue;
@@ -2908,7 +2973,11 @@ static void cs_collect_imports(CSLSPContext *ctx, TSNode root) {
29082973
strcmp(ck, "constructor_declaration") == 0 || strcmp(ck, "block") == 0) {
29092974
continue;
29102975
}
2976+
in_ns[top] = child_in_ns;
29112977
stack[top++] = c;
2978+
/* A file-scoped namespace covers the siblings that follow it. */
2979+
if (strcmp(ck, "file_scoped_namespace_declaration") == 0)
2980+
child_in_ns = true;
29122981
}
29132982
}
29142983
}
@@ -3749,12 +3818,16 @@ void cbm_run_cs_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
37493818
* type is not reachable by its simple name through a namespace). */
37503819
static const char *cs_top_level_namespace(const char *type_qn, const char *short_name,
37513820
const char *module_qn, const char *namespace_name) {
3752-
if (!type_qn || !short_name || !module_qn || !namespace_name || !namespace_name[0])
3821+
if (!type_qn || !short_name || !module_qn)
37533822
return NULL;
37543823
size_t mlen = strlen(module_qn);
37553824
if (strncmp(type_qn, module_qn, mlen) != 0 || type_qn[mlen] != '.' ||
37563825
strcmp(type_qn + mlen + 1, short_name) != 0)
37573826
return NULL;
3827+
/* Top-level C# type: its declared namespace, "" for the global namespace
3828+
* so step 8b can bind it, NULL when extraction could not tell (a parse
3829+
* error dissolved the namespace; see cs_type_decl_namespace). NULL stays
3830+
* invisible to step 8b, as do nested types. */
37583831
return namespace_name;
37593832
}
37603833

‎internal/cbm/lsp/cs_lsp.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ typedef struct {
6161
const char *local_name; /* alias name; "" for non-alias */
6262
const char *target_qn; /* dotted target QN */
6363
bool is_global; /* `global using` */
64+
/* Declared inside a namespace body rather than at compilation-unit level.
65+
* C# consults a namespace declaration's usings right after that
66+
* namespace's own members, but compilation-unit usings only after the
67+
* global namespace's members, so the two rank on opposite sides of it. */
68+
bool in_namespace;
6469
} CBMCSUsing;
6570

6671
/* CSLSPContext — per-file type-evaluation state. */
@@ -111,6 +116,11 @@ typedef struct {
111116
/* Recursion guard for cs_eval_expr_type. */
112117
int eval_depth;
113118

119+
/* Recursion guard for the nested-type qualifier walk of
120+
* cs_resolve_type_name: resolving `A.B` resolves the qualifier `A` as a
121+
* type first, and a `using A = A.B;` alias cycles back into it. */
122+
int qualifier_depth;
123+
114124
/* Debug mode (CBM_LSP_DEBUG env). */
115125
bool debug;
116126
} CSLSPContext;

0 commit comments

Comments
 (0)