Skip to content

Commit a5d2ea0

Browse files
authored
Merge pull request DeusData#2466 from DeusData/fix/issue-1153-instance-receivers
fix(cpp): resolve Class::member calls across files (DeusData#1153)
2 parents 3aef1c0 + cdd8c89 commit a5d2ea0

2 files changed

Lines changed: 485 additions & 0 deletions

File tree

‎internal/cbm/lsp/c_lsp.c‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,53 @@ static const CBMType *c_resolve_name_to_type(CLSPContext *ctx, const char *name)
10251025
return cbm_type_named(ctx->arena, name);
10261026
}
10271027

1028+
/* Resolve the class of a qualified member `Scope::member` -- scope_qn is the
1029+
* dotted scope ("SecdManager", "ns.Widget") -- to the QN of ONE registered
1030+
* class, or NULL. The bare and caller-module spellings come first. A class
1031+
* declared in another file is registered under that file's module
1032+
* ("<proj>.app.src.SecdManager.SecdManager"), which neither spelling reaches,
1033+
* so then every registered type whose QN ends in ".<scope_qn>" at a segment
1034+
* boundary is a candidate. More than one distinct candidate (two libraries
1035+
* each declaring `Twin`) names no single class: the call stays unresolved
1036+
* rather than bind one of them by guess. */
1037+
static const char *c_resolve_scope_class_qn(CLSPContext *ctx, const char *scope_qn) {
1038+
if (!ctx->registry || !scope_qn || !scope_qn[0]) {
1039+
return NULL;
1040+
}
1041+
if (cbm_registry_lookup_type(ctx->registry, scope_qn)) {
1042+
return scope_qn;
1043+
}
1044+
if (ctx->module_qn) {
1045+
const char *mod_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->module_qn, scope_qn);
1046+
if (cbm_registry_lookup_type(ctx->registry, mod_qn)) {
1047+
return mod_qn;
1048+
}
1049+
}
1050+
const char *last_dot = strrchr(scope_qn, '.');
1051+
const char *short_name = last_dot ? last_dot + 1 : scope_qn;
1052+
size_t scope_len = strlen(scope_qn);
1053+
const char *found = NULL;
1054+
CBMTypeShortIter it;
1055+
cbm_registry_types_by_short_name_chain(ctx->registry, short_name, &it);
1056+
int i;
1057+
while ((i = cbm_type_short_iter_next(&it)) >= 0) {
1058+
const char *q = it.reg->types[i].qualified_name;
1059+
size_t qlen = q ? strlen(q) : 0;
1060+
if (qlen <= scope_len || q[qlen - scope_len - 1] != '.' ||
1061+
strcmp(q + qlen - scope_len, scope_qn) != 0) {
1062+
continue;
1063+
}
1064+
if (found && strcmp(found, q) != 0) {
1065+
return NULL;
1066+
}
1067+
found = q;
1068+
}
1069+
return found;
1070+
}
1071+
1072+
static const CBMRegisteredFunc *c_lookup_scope_member(CLSPContext *ctx, const char *class_qn,
1073+
const char *member_name);
1074+
10281075
// ============================================================================
10291076
// c_parse_type_node: AST type node -> CBMType
10301077
// ============================================================================
@@ -1821,6 +1868,16 @@ static const CBMType *c_eval_expr_type_inner(CLSPContext *ctx, TSNode node) {
18211868
}
18221869
}
18231870
}
1871+
1872+
// Class::member of a class declared in another file: its signature
1873+
// carries the return type a chained or auto receiver dispatches on.
1874+
const char *scope_class =
1875+
c_resolve_scope_class_qn(ctx, cbm_arena_strndup(ctx->arena, qn, prefix_len));
1876+
const CBMRegisteredFunc *sm =
1877+
scope_class ? c_lookup_scope_member(ctx, scope_class, dot + 1) : NULL;
1878+
if (sm && sm->signature) {
1879+
return sm->signature;
1880+
}
18241881
}
18251882

18261883
return cbm_type_unknown();
@@ -2860,6 +2917,15 @@ const CBMRegisteredFunc *c_lookup_member(CLSPContext *ctx, const char *type_qn,
28602917
return c_lookup_member_depth(ctx, type_qn, member_name, 0);
28612918
}
28622919

2920+
/* A member of a class c_resolve_scope_class_qn already pinned down: direct,
2921+
* then through its aliases and base classes. Entering below depth 0 skips the
2922+
* short-name fallback, which would re-anchor the lookup onto a same-named
2923+
* class elsewhere -- the scope names exactly this class. */
2924+
static const CBMRegisteredFunc *c_lookup_scope_member(CLSPContext *ctx, const char *class_qn,
2925+
const char *member_name) {
2926+
return c_lookup_member_depth(ctx, class_qn, member_name, 1);
2927+
}
2928+
28632929
// True if any BASE class of type_qn (not type_qn itself) declares member_name —
28642930
// i.e. a method found directly on type_qn is an OVERRIDE of an inherited method.
28652931
// This mirrors the existing virtual-dispatch notion (a derived override of a base
@@ -4142,6 +4208,24 @@ static void c_resolve_calls_in_node_inner(CLSPContext *ctx, TSNode node) {
41424208
goto recurse;
41434209
}
41444210
}
4211+
// Class::method of a class declared in another file (#1153).
4212+
if (dot) {
4213+
const char *scope_class = c_resolve_scope_class_qn(
4214+
ctx, cbm_arena_strndup(ctx->arena, qn, (size_t)(dot - qn)));
4215+
const CBMRegisteredFunc *sm = NULL;
4216+
if (scope_class) {
4217+
sm = cbm_registry_lookup_method_by_types(ctx->registry, scope_class,
4218+
dot + 1, arg_types, arg_count);
4219+
if (!sm) {
4220+
sm = c_lookup_scope_member(ctx, scope_class, dot + 1);
4221+
}
4222+
}
4223+
if (sm) {
4224+
c_emit_resolved_call_at(ctx, sm->qualified_name, "lsp_scoped", 0.95f,
4225+
node);
4226+
goto recurse;
4227+
}
4228+
}
41454229
c_emit_unresolved_call(ctx, qn, "scoped_not_in_registry");
41464230
}
41474231
goto recurse;

0 commit comments

Comments
 (0)