@@ -74,6 +74,8 @@ static char *cs_node_text_cached(CSLSPContext *ctx, TSNode node);
7474static const CBMType * cs_unwrap_task (CSLSPContext * ctx , const CBMType * t );
7575static const CBMType * cs_unwrap_nullable (const CBMType * t );
7676static const char * cs_resolve_callable_value (CSLSPContext * ctx , TSNode node , TSNode * callable_leaf );
77+ static const char * cs_top_level_namespace (const char * type_qn , const char * short_name ,
78+ const char * module_qn , const char * namespace_name );
7779
7880/* ── small helpers ──────────────────────────────────────────────── */
7981
@@ -356,6 +358,72 @@ static const char *cs_try_type_qn(CSLSPContext *ctx, const char *qn) {
356358 return NULL ;
357359}
358360
361+ /* Namespaces through which `qualifier` (the part of a type reference before
362+ * its simple name, NULL for a simple name) can reach a top-level type, in C#
363+ * 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`). */
367+ static int cs_visible_namespaces (CSLSPContext * ctx , const char * qualifier , const char * * out ,
368+ int cap ) {
369+ int n = 0 ;
370+ const char * ns = ctx -> namespace_count > 0 ? cs_namespace_qn (ctx ) : NULL ;
371+ while (ns && * ns && n < cap ) {
372+ out [n ++ ] = qualifier ? cbm_arena_sprintf (ctx -> arena , "%s.%s" , ns , qualifier ) : ns ;
373+ const char * dot = strrchr (ns , '.' );
374+ ns = dot ? cbm_arena_strndup (ctx -> arena , ns , (size_t )(dot - ns )) : NULL ;
375+ }
376+ if (qualifier ) {
377+ if (n < cap )
378+ out [n ++ ] = qualifier ;
379+ return n ;
380+ }
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 ;
387+ }
388+
389+ /* Bind a (possibly namespace-qualified) type reference to the project type
390+ * whose DECLARED namespace the current file can see, preferring the earliest
391+ * namespace in C# lookup order; equal ranks keep registration order. NULL when
392+ * no top-level type with that simple name is visible (#2120). */
393+ static const char * cs_resolve_by_declared_namespace (CSLSPContext * ctx , const char * name ) {
394+ if (!ctx -> registry || !name || !* name )
395+ return NULL ;
396+ const char * simple = cs_short_name (name );
397+ if (!simple || !* simple )
398+ return NULL ;
399+ const char * qualifier =
400+ simple > name ? cbm_arena_strndup (ctx -> arena , name , (size_t )(simple - name - 1 )) : NULL ;
401+ enum { CS_VISIBLE_NS_MAX = 128 };
402+ const char * visible [CS_VISIBLE_NS_MAX ];
403+ int visible_count = cs_visible_namespaces (ctx , qualifier , visible , CS_VISIBLE_NS_MAX );
404+ if (visible_count == 0 )
405+ return NULL ;
406+
407+ const CBMRegisteredType * best = NULL ;
408+ int best_rank = visible_count ;
409+ CBMTypeShortIter it ;
410+ cbm_registry_types_by_short_name_chain (ctx -> registry , simple , & it );
411+ int i ;
412+ while ((i = cbm_type_short_iter_next (& it )) >= 0 ) {
413+ const CBMRegisteredType * cand = & it .reg -> types [i ];
414+ if (!cand -> namespace_qn || !cand -> short_name || strcmp (cand -> short_name , simple ) != 0 )
415+ continue ;
416+ for (int r = 0 ; r < best_rank ; r ++ ) {
417+ if (strcmp (cand -> namespace_qn , visible [r ]) == 0 ) {
418+ best = cand ;
419+ best_rank = r ;
420+ break ;
421+ }
422+ }
423+ }
424+ return best ? best -> qualified_name : NULL ;
425+ }
426+
359427/* Returns a fully-qualified type name resolved against:
360428 * 1. predefined alias (int -> System.Int32)
361429 * 2. type-parameter substitution map
@@ -365,7 +433,9 @@ static const char *cs_try_type_qn(CSLSPContext *ctx, const char *qn) {
365433 * 6. module-prefixed (file-local QN that the unified extractor produced)
366434 * 7. each `using namespace X` prefix
367435 * 8. each `using A = X` alias substitution
368- * 9. fall back to bare name (registry will fail; pipeline drops the call)
436+ * 8b. declared namespace of a top-level project type (enclosing namespaces,
437+ * then `using` namespaces)
438+ * 9. short-name fallback, then the bare name (registry miss drops the call)
369439 */
370440const char * cs_resolve_type_name (CSLSPContext * ctx , const char * raw ) {
371441 if (!raw || !* raw )
@@ -457,6 +527,11 @@ const char *cs_resolve_type_name(CSLSPContext *ctx, const char *raw) {
457527 char * target_bare = cs_strip_generic_args (ctx -> arena , u -> target_qn );
458528 if (target_bare && cs_lookup_type_qn (ctx , target_bare ))
459529 return target_bare ;
530+ /* A project type is registered under its path-derived QN, so
531+ * `using A = Ns.Type;` binds through the declared namespace. */
532+ const char * declared = cs_resolve_by_declared_namespace (ctx , target_bare );
533+ if (declared )
534+ return declared ;
460535 return cbm_arena_strdup (ctx -> arena , u -> target_qn );
461536 }
462537 if (bare [alias_len ] == '.' ) {
@@ -470,6 +545,15 @@ const char *cs_resolve_type_name(CSLSPContext *ctx, const char *raw) {
470545 }
471546 }
472547
548+ /* 8b. Declared-namespace binding for project types (#2120): their QNs are
549+ * path-derived, so steps 5 and 7 cannot hit them; bind by the namespace
550+ * each top-level type declares, in C# lookup order. */
551+ {
552+ const char * declared = cs_resolve_by_declared_namespace (ctx , bare );
553+ if (declared )
554+ return declared ;
555+ }
556+
473557 /* 9. Short-name fallback: scan the registry for any type whose
474558 * short_name equals `bare` and whose QN shares the longest namespace
475559 * prefix with the file's namespace stack. This recovers cross-file
@@ -525,6 +609,28 @@ const char *cs_resolve_type_name(CSLSPContext *ctx, const char *raw) {
525609 return bare ;
526610}
527611
612+ /* Other top-level declarations of the type `t` names: same simple name, same
613+ * declared namespace, different entry. Registration order; at most `cap`. */
614+ static int cs_namespace_twins (CSLSPContext * ctx , const CBMRegisteredType * t ,
615+ const CBMRegisteredType * * out , int cap ) {
616+ if (!ctx -> registry || !t || !t -> namespace_qn || !t -> short_name )
617+ return 0 ;
618+ int n = 0 ;
619+ CBMTypeShortIter it ;
620+ cbm_registry_types_by_short_name_chain (ctx -> registry , t -> short_name , & it );
621+ int i ;
622+ while (n < cap && (i = cbm_type_short_iter_next (& it )) >= 0 ) {
623+ const CBMRegisteredType * c = & it .reg -> types [i ];
624+ if (!c -> namespace_qn || !c -> short_name || !c -> qualified_name ||
625+ strcmp (c -> short_name , t -> short_name ) != 0 ||
626+ strcmp (c -> namespace_qn , t -> namespace_qn ) != 0 ||
627+ strcmp (c -> qualified_name , t -> qualified_name ) == 0 )
628+ continue ;
629+ out [n ++ ] = c ;
630+ }
631+ return n ;
632+ }
633+
528634/* Look up a method on a type, walking inheritance chain. */
529635const CBMRegisteredFunc * cs_lookup_method (CSLSPContext * ctx , const char * type_qn ,
530636 const char * method_name ) {
@@ -535,19 +641,34 @@ const CBMRegisteredFunc *cs_lookup_method(CSLSPContext *ctx, const char *type_qn
535641 if (f )
536642 return f ;
537643
538- /* Walk inheritance chain (base + interfaces + transitive bases). */
539644 const CBMRegisteredType * t = cs_lookup_type_qn (ctx , type_qn );
540645 if (!t )
541646 return NULL ;
542647
648+ /* The other declarations of this same type -- `partial` pieces in other
649+ * files, reference-assembly copies -- are separate registry entries
650+ * (path-derived QNs) but one C# type: a member declared on any of them is
651+ * a member of this one, and so is a base declared on any of them. */
652+ const CBMRegisteredType * twins [CS_LSP_PARENT_WALK_MAX ] = {0 };
653+ int twin_count = cs_namespace_twins (ctx , t , twins , CS_LSP_PARENT_WALK_MAX );
654+ for (int k = 0 ; k < twin_count ; k ++ ) {
655+ f = cbm_registry_lookup_method (ctx -> registry , twins [k ]-> qualified_name , method_name );
656+ if (f )
657+ return f ;
658+ }
659+
660+ /* Walk inheritance chain (base + interfaces + transitive bases). */
543661 const char * visited [CS_LSP_PARENT_WALK_MAX ];
544662 int visited_count = 0 ;
545663 const char * frontier [CS_LSP_PARENT_WALK_MAX ];
546664 int frontier_count = 0 ;
547665
548- if (t -> embedded_types ) {
549- for (int i = 0 ; t -> embedded_types [i ] && frontier_count < CS_LSP_PARENT_WALK_MAX ; i ++ ) {
550- frontier [frontier_count ++ ] = t -> embedded_types [i ];
666+ for (int k = -1 ; k < twin_count ; k ++ ) {
667+ const CBMRegisteredType * decl = k < 0 ? t : twins [k ];
668+ for (int i = 0 ; decl -> embedded_types && decl -> embedded_types [i ] &&
669+ frontier_count < CS_LSP_PARENT_WALK_MAX ;
670+ i ++ ) {
671+ frontier [frontier_count ++ ] = decl -> embedded_types [i ];
551672 }
552673 }
553674
@@ -3469,6 +3590,8 @@ void cbm_run_cs_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
34693590 memset (& rt , 0 , sizeof (rt ));
34703591 rt .qualified_name = d -> qualified_name ;
34713592 rt .short_name = d -> name ;
3593+ rt .namespace_qn =
3594+ cs_top_level_namespace (d -> qualified_name , d -> name , module_qn , d -> decl_namespace );
34723595 rt .is_interface = (strcmp (d -> label , "Interface" ) == 0 );
34733596 if (d -> base_classes ) {
34743597 int bc = 0 ;
@@ -3620,6 +3743,21 @@ void cbm_run_cs_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
36203743
36213744/* ── cross-file entry ───────────────────────────────────────────── */
36223745
3746+ /* The type's declared `namespace_name` when `type_qn` is a TOP-LEVEL type of
3747+ * the file whose module QN is `module_qn` (top-level C# type QNs are exactly
3748+ * module.Name; nested ones are module.Outer.Name and stay NULL, since a nested
3749+ * type is not reachable by its simple name through a namespace). */
3750+ static const char * cs_top_level_namespace (const char * type_qn , const char * short_name ,
3751+ const char * module_qn , const char * namespace_name ) {
3752+ if (!type_qn || !short_name || !module_qn || !namespace_name || !namespace_name [0 ])
3753+ return NULL ;
3754+ size_t mlen = strlen (module_qn );
3755+ if (strncmp (type_qn , module_qn , mlen ) != 0 || type_qn [mlen ] != '.' ||
3756+ strcmp (type_qn + mlen + 1 , short_name ) != 0 )
3757+ return NULL ;
3758+ return namespace_name ;
3759+ }
3760+
36233761/* Register one batch of CBMLSPDef[] into a registry. Shared by the
36243762 * per-file cross-LSP path and the Tier 2 pre-built registry builder.
36253763 * Def-driven (no per-file AST mutation) so deterministic per def set. */
@@ -3636,6 +3774,8 @@ static void cs_register_lsp_defs(CBMArena *arena, CBMTypeRegistry *reg, CBMLSPDe
36363774 memset (& rt , 0 , sizeof (rt ));
36373775 rt .qualified_name = d -> qualified_name ;
36383776 rt .short_name = d -> short_name ;
3777+ rt .namespace_qn = cs_top_level_namespace (d -> qualified_name , d -> short_name ,
3778+ d -> def_module_qn , d -> namespace_name );
36393779 rt .is_interface = d -> is_interface || (strcmp (d -> label , "Interface" ) == 0 );
36403780 if (d -> embedded_types && * d -> embedded_types ) {
36413781 /* Parse "|"-separated list. */
0 commit comments