Skip to content

Commit e1bf7cc

Browse files
committed
fix(cpp): namespace-qualified QNs so ADL and namespace lookups resolve
A symbol declared in `namespace ns` was collapsed to the file scope (`proj.file.serialize`, not `proj.file.ns.serialize`) because the def walk treated namespace_definition as a module-level container that contributes no QN segment. With the namespace absent from the QN, an unqualified `serialize(d)` resolved DIRECTLY to the top-level name and the argument-dependent-lookup path (c_adl_resolve, which reads the namespace out of the argument type's QN) was never reached. Make a namespace contribute a QN segment in the def walk (it extends the enclosing scope like a class but emits no def of its own; its members — both free functions and nested classes — are walked under the extended scope). Mirror it in c_lsp: - c_process_function qualifies a namespace-scoped free function with current_namespace instead of the file module, matching the def QN. - c_lookup_member gains a short-name fallback so a type referenced by its file-scoped or bare name (e.g. the return type of a namespace-scoped factory used outside that namespace) still resolves to its namespace-qualified registered QN. - c_adl_resolve also tries the module-prefixed form of an argument type's namespace, since `ns::Data` evaluates to the bare namespace `ns` while the overload is registered under `<module>.ns.serialize`. C#/PHP need the same treatment paired with their own resolvers (a def-only change breaks their existing namespace handling) and are done separately. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 5b60045 commit e1bf7cc

2 files changed

Lines changed: 98 additions & 3 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2899,6 +2899,18 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
28992899
* proj.myapp.db.Func, not proj.myapp.db.conn.Func). Other langs unchanged. */
29002900
def.qualified_name =
29012901
cbm_fqn_compute_source_lang(a, ctx->project, ctx->rel_path, name, ctx->language);
2902+
/* A free function declared inside a namespace (C++/C#/PHP) is qualified by
2903+
* the namespace scope the def walk carries (enclosing_class_qn was extended
2904+
* by is_namespace_scope_kind), so `ns::serialize` is `proj.file.ns.serialize`
2905+
* — without this it collapses to the file scope and namespace-aware
2906+
* resolution (ADL, namespace-function lookup) can never see it. Class methods
2907+
* never reach here (they go through extract_class_methods), so a set
2908+
* enclosing scope here is always a namespace. The out-of-line method path
2909+
* below overrides this for `Ns::Cls::method` definitions. */
2910+
if (ctx->enclosing_class_qn &&
2911+
(ctx->language == CBM_LANG_CPP || ctx->language == CBM_LANG_CUDA)) {
2912+
def.qualified_name = cbm_arena_sprintf(a, "%s.%s", ctx->enclosing_class_qn, name);
2913+
}
29022914
def.label = "Function";
29032915
def.file_path = ctx->rel_path;
29042916
def.start_line = ts_node_start_point(node).row + TS_LINE_OFFSET;
@@ -5453,6 +5465,20 @@ static bool is_template_class_node(TSNode node, CBMLanguage lang) {
54535465
}
54545466

54555467
// Compute the enclosing class QN for a class node (for nested class context).
5468+
/* A namespace contributes a QN segment so a symbol declared in `namespace ns`
5469+
* is `proj.file.ns.sym`, not a top-level `proj.file.sym`. Without the namespace
5470+
* in the QN, namespace-aware resolution (C++ ADL) is starved: a bare call
5471+
* collapses to the file scope and resolves directly instead. Unlike a class, a
5472+
* namespace emits no def of its own — it only extends the enclosing scope for
5473+
* its members. C#/PHP need the same treatment paired with their LSP resolvers
5474+
* (a def-only change breaks their existing namespace handling), done separately. */
5475+
static bool is_namespace_scope_kind(CBMLanguage lang, const char *kind) {
5476+
if (lang == CBM_LANG_CPP || lang == CBM_LANG_CUDA) {
5477+
return strcmp(kind, "namespace_definition") == 0;
5478+
}
5479+
return false;
5480+
}
5481+
54565482
static const char *compute_class_qn(CBMExtractCtx *ctx, TSNode node, const char *saved_enclosing) {
54575483
TSNode name_node = ts_node_child_by_field_name(node, TS_FIELD("name"));
54585484
if (ts_node_is_null(name_node) && ctx->language == CBM_LANG_OBJC) {
@@ -6002,6 +6028,21 @@ static void walk_defs(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec,
60026028
continue;
60036029
}
60046030

6031+
/* A namespace extends the enclosing scope (so members are QN-qualified by
6032+
* it) without being a def itself. Push its children (its declaration_list
6033+
* body and any nested namespaces) under the extended scope so each member
6034+
* is walked normally — functions AND classes, unlike a class body which
6035+
* routes methods through extract_class_methods. Do NOT emit a def or run
6036+
* the class/func paths on the namespace node itself. */
6037+
if (is_namespace_scope_kind(ctx->language, kind)) {
6038+
const char *new_enclosing = compute_class_qn(ctx, node, frame.enclosing_class_qn);
6039+
uint32_t nsc = ts_node_child_count(node);
6040+
for (int i = (int)nsc - SKIP_CHAR; i >= 0 && top < CBM_WALK_DEFS_STACK_CAP; i--) {
6041+
stack[top++] = (walk_defs_frame_t){ts_node_child(node, (uint32_t)i), new_enclosing};
6042+
}
6043+
continue;
6044+
}
6045+
60056046
if (cbm_kind_in_set(node, spec->class_node_types)) {
60066047
extract_class_def(ctx, node, spec);
60076048
const char *new_enclosing = compute_class_qn(ctx, node, frame.enclosing_class_qn);

‎internal/cbm/lsp/c_lsp.c‎

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -744,9 +744,17 @@ static const char *c_adl_resolve(CLSPContext *ctx, const char *name, TSNode call
744744
namespaces[ns_count++] = ns;
745745
}
746746

747-
// Try each namespace
747+
// Try each namespace, then the module-prefixed form of it. An argument type
748+
// written as `ns::Data` evaluates to the namespace QN `ns`, but the function
749+
// is registered under the module-qualified `<module>.ns.serialize`; without
750+
// the module-prefixed retry the namespace-scoped overload is never found.
748751
for (int i = 0; i < ns_count; i++) {
749752
const CBMRegisteredFunc *f = cbm_registry_lookup_symbol(ctx->registry, namespaces[i], name);
753+
if (!f && ctx->module_qn) {
754+
const char *prefixed =
755+
cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->module_qn, namespaces[i]);
756+
f = cbm_registry_lookup_symbol(ctx->registry, prefixed, name);
757+
}
750758
if (f)
751759
return f->qualified_name;
752760
}
@@ -2559,6 +2567,45 @@ static const CBMRegisteredFunc *c_lookup_member_depth(CLSPContext *ctx, const ch
25592567
}
25602568
}
25612569

2570+
/* Namespaced-type short-name fallback: a type name that resolves nowhere may
2571+
* be a type declared inside a namespace whose registered QN carries the
2572+
* namespace ("<module>.<ns>.Logger"), while the use site only knew the
2573+
* file-scoped "<module>.Logger" or the bare "Logger" (e.g. the return type of
2574+
* a namespace-scoped factory used outside that namespace). Resolve by the
2575+
* SHORT name (last segment) against the registry and retry with the full QN.
2576+
* Reached only after the direct/module/alias/base lookups all miss; prefers
2577+
* an in-module match. Mirrors the C# short-name type fallback. */
2578+
if (depth == 0 && ctx->registry) {
2579+
const char *dot = strrchr(type_qn, '.');
2580+
const char *shortn = dot ? dot + 1 : type_qn;
2581+
size_t slen = strlen(shortn);
2582+
const char *best_qn = NULL;
2583+
for (int i = 0; i < ctx->registry->type_count; i++) {
2584+
const char *q = ctx->registry->types[i].qualified_name;
2585+
if (!q) {
2586+
continue;
2587+
}
2588+
size_t qlen = strlen(q);
2589+
if (qlen <= slen + 1 || q[qlen - slen - 1] != '.' ||
2590+
strcmp(q + qlen - slen, shortn) != 0) {
2591+
continue;
2592+
}
2593+
if (strcmp(q, type_qn) == 0) {
2594+
continue; // already tried as-is above
2595+
}
2596+
best_qn = q;
2597+
if (ctx->module_qn && strncmp(q, ctx->module_qn, strlen(ctx->module_qn)) == 0) {
2598+
break; // prefer a match in the current module
2599+
}
2600+
}
2601+
if (best_qn) {
2602+
f = c_lookup_member_depth(ctx, best_qn, member_name, depth + 1);
2603+
if (f) {
2604+
return f;
2605+
}
2606+
}
2607+
}
2608+
25622609
return NULL;
25632610
}
25642611

@@ -4204,8 +4251,15 @@ static void c_process_function(CLSPContext *ctx, TSNode func_node) {
42044251
if (ctx->enclosing_class_qn && saved_class_qn == ctx->enclosing_class_qn &&
42054252
!strchr(func_qn, '.')) {
42064253
func_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->enclosing_class_qn, func_qn);
4207-
} else if (ctx->module_qn && !strchr(func_qn, '.')) {
4208-
func_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->module_qn, func_qn);
4254+
} else if (!strchr(func_qn, '.')) {
4255+
/* A free function in a namespace is qualified by the namespace scope
4256+
* (current_namespace is module_qn.ns), matching the def QN the extractor
4257+
* now produces; outside any namespace this falls back to the file module
4258+
* so non-namespaced free functions are unchanged. */
4259+
const char *scope = ctx->current_namespace ? ctx->current_namespace : ctx->module_qn;
4260+
if (scope) {
4261+
func_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", scope, func_qn);
4262+
}
42094263
}
42104264
ctx->enclosing_func_qn = func_qn;
42114265

0 commit comments

Comments
 (0)