Skip to content

Commit edb4a6e

Browse files
authored
Merge pull request DeusData#2295 from MopicMP/fix/java-record-accessor-calls
fix(java): resolve record accessor calls, and never bind a call to data
2 parents 88a1d1d + bff6ce9 commit edb4a6e

14 files changed

Lines changed: 895 additions & 18 deletions

‎internal/cbm/cbm.h‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,10 @@ typedef struct {
298298
* are bound to the owning result's source identity before cross-file use. */
299299
uint32_t test_name_start_byte, test_name_end_byte;
300300
uint32_t test_body_start_byte, test_body_end_byte;
301-
/* Documents only: facts a document definition adds to its node's
302-
* properties, as a NULL-terminated key, value, key, value, ... list (an
303-
* ADR's id, status, date, deciders, title). Keys are fixed literals; the
301+
/* Facts a definition adds to its node's properties, as a NULL-terminated
302+
* key, value, key, value, ... list: a document's (an ADR's id, status,
303+
* date, deciders, title), or CBM_PROP_GENERATED on a member the compiler
304+
* writes and the source never spells. Keys are fixed literals; the
304305
* values are escaped where the properties are written. Tail field:
305306
* zero-init stays valid. */
306307
const char **extra_props;
@@ -321,6 +322,13 @@ typedef struct {
321322
const char *alias_target;
322323
} CBMDefinition;
323324

325+
/* extra_props key of a generated member, one the compiler writes and the
326+
* source never spells: a Java record's implicit accessor carries
327+
* `generated: record`. The node keeps the property, and the pipeline registers
328+
* such a definition by qualified name only, so no short-name guess can land on
329+
* it (pipeline.h cbm_def_props_generated). */
330+
#define CBM_PROP_GENERATED "generated"
331+
324332
/* Argument captured from a call expression */
325333
typedef struct {
326334
const char *expr; // raw expression text ("payload.info", "MY_URL", "'hello'")

‎internal/cbm/extract_defs.c‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ static void extract_class_fields(CBMExtractCtx *ctx, TSNode class_node, const ch
230230
const CBMLangSpec *spec);
231231
static TSNode find_class_body(TSNode class_node, CBMLanguage lang);
232232
static void extract_enum_members(CBMExtractCtx *ctx, TSNode node, const char *class_qn);
233+
static void extract_record_accessors(CBMExtractCtx *ctx, TSNode node, const char *class_qn);
233234
static void extract_elixir_call(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec);
234235

235236
// --- Helpers ---
@@ -8317,6 +8318,10 @@ static void emit_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *s
83178318
extract_enum_members(ctx, node, class_qn);
83188319
}
83198320

8321+
if (ctx->language == CBM_LANG_JAVA && strcmp(kind, "record_declaration") == 0) {
8322+
extract_record_accessors(ctx, node, class_qn);
8323+
}
8324+
83208325
// Extract methods inside the class
83218326
int methods_from = ctx->result->defs.count;
83228327
extract_class_methods(ctx, node, class_qn, spec);
@@ -9642,6 +9647,121 @@ static void extract_enum_members(CBMExtractCtx *ctx, TSNode node, const char *cl
96429647
}
96439648
}
96449649

9650+
/* Whether the record body declares this accessor itself (JLS §8.10.3 lets a
9651+
* record override any of them), so the implicit one is not emitted twice. */
9652+
static bool record_declares_accessor(CBMExtractCtx *ctx, TSNode node, const char *comp_name) {
9653+
TSNode body = find_class_body(node, ctx->language);
9654+
if (ts_node_is_null(body)) {
9655+
return false;
9656+
}
9657+
uint32_t mc = ts_node_named_child_count(body);
9658+
for (uint32_t mi = 0; mi < mc; mi++) {
9659+
TSNode member = ts_node_named_child(body, mi);
9660+
if (strcmp(ts_node_type(member), "method_declaration") != 0) {
9661+
continue;
9662+
}
9663+
TSNode mname = ts_node_child_by_field_name(member, TS_FIELD("name"));
9664+
if (ts_node_is_null(mname)) {
9665+
continue;
9666+
}
9667+
char *name = cbm_node_text(ctx->arena, mname, ctx->source);
9668+
if (!name || strcmp(name, comp_name) != 0) {
9669+
continue;
9670+
}
9671+
/* Same name, no parameters — that is the accessor. */
9672+
TSNode params = ts_node_child_by_field_name(member, TS_FIELD("parameters"));
9673+
if (ts_node_is_null(params)) {
9674+
return true;
9675+
}
9676+
/* Comments are named extras, not parameters. */
9677+
bool has_parameters = false;
9678+
uint32_t pc = ts_node_named_child_count(params);
9679+
for (uint32_t pi = 0; pi < pc; pi++) {
9680+
if (!ts_node_is_extra(ts_node_named_child(params, pi))) {
9681+
has_parameters = true;
9682+
break;
9683+
}
9684+
}
9685+
if (!has_parameters) {
9686+
return true;
9687+
}
9688+
}
9689+
return false;
9690+
}
9691+
9692+
/* Java records: the compiler writes one accessor per component, so `p.x()`
9693+
* calls a method that appears nowhere in the source. Without a definition for
9694+
* it the call has nothing to resolve to and falls through to the project-wide
9695+
* name registry, which binds it to the first `x` in the tree — typically an
9696+
* unrelated class's private field. Emitting the accessors makes the record's
9697+
* own API visible, exactly as the language defines it (JLS §8.10.3). */
9698+
static void extract_record_accessors(CBMExtractCtx *ctx, TSNode node, const char *class_qn) {
9699+
CBMArena *a = ctx->arena;
9700+
TSNode params = ts_node_child_by_field_name(node, TS_FIELD("parameters"));
9701+
if (ts_node_is_null(params)) {
9702+
return;
9703+
}
9704+
uint32_t pc = ts_node_named_child_count(params);
9705+
for (uint32_t pi = 0; pi < pc; pi++) {
9706+
TSNode comp = ts_node_named_child(params, pi);
9707+
bool spread = strcmp(ts_node_type(comp), "spread_parameter") == 0;
9708+
if (!spread && strcmp(ts_node_type(comp), "formal_parameter") != 0) {
9709+
continue;
9710+
}
9711+
TSNode cname = ts_node_child_by_field_name(comp, TS_FIELD("name"));
9712+
TSNode ctype = ts_node_child_by_field_name(comp, TS_FIELD("type"));
9713+
if (spread) {
9714+
/* Java varargs have an unnamed type child and a nested declarator. */
9715+
TSNode decl = cbm_find_child_by_kind(comp, "variable_declarator");
9716+
if (ts_node_is_null(decl)) {
9717+
continue;
9718+
}
9719+
cname = ts_node_child_by_field_name(decl, TS_FIELD("name"));
9720+
uint32_t nc = ts_node_named_child_count(comp);
9721+
for (uint32_t ci = 0; ci < nc; ci++) {
9722+
TSNode child = ts_node_named_child(comp, ci);
9723+
if (!ts_node_is_extra(child) && strcmp(ts_node_type(child), "modifiers") != 0) {
9724+
ctype = child;
9725+
break;
9726+
}
9727+
}
9728+
}
9729+
if (ts_node_is_null(cname)) {
9730+
continue;
9731+
}
9732+
char *comp_name = cbm_node_text(a, cname, ctx->source);
9733+
if (!comp_name || !comp_name[0] || record_declares_accessor(ctx, node, comp_name)) {
9734+
continue;
9735+
}
9736+
CBMDefinition adef;
9737+
memset(&adef, 0, sizeof(adef));
9738+
adef.name = comp_name;
9739+
adef.qualified_name = cbm_arena_sprintf(a, "%s.%s", class_qn, comp_name);
9740+
adef.label = "Method";
9741+
adef.file_path = ctx->rel_path;
9742+
adef.start_line = ts_node_start_point(comp).row + TS_LINE_OFFSET;
9743+
adef.end_line = ts_node_end_point(comp).row + TS_LINE_OFFSET;
9744+
adef.lines = 1;
9745+
adef.parent_class = class_qn;
9746+
adef.is_exported = true;
9747+
if (!ts_node_is_null(ctype)) {
9748+
const char *type = cbm_node_text(a, ctype, ctx->source);
9749+
adef.return_type = spread && type ? cbm_arena_sprintf(a, "%s[]", type) : type;
9750+
}
9751+
/* The compiler writes this member and the source never spells it: its
9752+
* node says so, and the pipeline registers it by qualified name only,
9753+
* so no short-name guess can land on it. */
9754+
const char **kv = (const char **)cbm_arena_alloc(a, CBM_SZ_3 * sizeof(char *));
9755+
if (kv) {
9756+
kv[0] = CBM_PROP_GENERATED;
9757+
kv[SKIP_ONE] = "record";
9758+
kv[PAIR_LEN] = NULL;
9759+
}
9760+
adef.extra_props = kv;
9761+
cbm_defs_push(&ctx->result->defs, a, adef);
9762+
}
9763+
}
9764+
96459765
/* Resolve the identifier node from a destructure pattern child.
96469766
* pair_pattern → value field; shorthand/identifier → itself; others → first named child. */
96479767
static TSNode destructure_ident(TSNode pat_child) {

‎src/pipeline/lsp_resolve.h‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,10 +1281,18 @@ static inline const cbm_gbuf_node_t *cbm_pipeline_lsp_target_node_policy(
12811281
if (strcmp(cand->label, "Function") != 0 && strcmp(cand->label, "Method") != 0) {
12821282
continue;
12831283
}
1284-
if (!unique_callable) {
1285-
unique_callable = cand;
1286-
} else if (unique_callable->id != cand->id) {
1287-
callable_ambiguous = true;
1284+
/* A generated member (a Java record's implicit accessor) is registered
1285+
* by qualified name only: it joins through its owner's Class.method
1286+
* tail below, never on its leaf name alone -- `Objects.nonNull(x)` is
1287+
* no call of some record's nonNull(). Nor does it make a same-named
1288+
* callable ambiguous: the leaf fallback sees the graph as if it were
1289+
* not there. */
1290+
if (!cbm_node_props_generated(cand->properties_json)) {
1291+
if (!unique_callable) {
1292+
unique_callable = cand;
1293+
} else if (unique_callable->id != cand->id) {
1294+
callable_ambiguous = true;
1295+
}
12881296
}
12891297
if (!cbm_pipeline_qn_class_method_tail_eq(cand->qualified_name, callee_tail)) {
12901298
continue;

‎src/pipeline/pass_calls.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,11 @@ static bool calls_emit_lsp_target(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
997997
if (!target_node || source_node->id == target_node->id) {
998998
return false;
999999
}
1000+
/* LSP textual fallbacks can also name data, as with static imports. The
1001+
* call is handled: no edge, and no registry fallback after it. */
1002+
if (cbm_java_suppress_call_to_data_member(lang == CBM_LANG_JAVA, target_node->label)) {
1003+
return true;
1004+
}
10001005
cbm_resolution_t res = {0};
10011006
/* Keep the canonical graph QN, including project prefixes added by fallback. */
10021007
res.qualified_name = target_node->qualified_name;
@@ -1333,6 +1338,12 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call, const CBM
13331338
return calls_emit_route_call(ctx, call, source_node, module_qn, imp_keys, imp_vals,
13341339
imp_count, route_mount, lang, evidence);
13351340
}
1341+
/* A Java call never targets data — see cbm_java_suppress_call_to_data_member.
1342+
* The language gate lives here, as with the guards above, and MUST match
1343+
* pass_parallel.c exactly or the two resolvers diverge. */
1344+
if (cbm_java_suppress_call_to_data_member(lang == CBM_LANG_JAVA, target_node->label)) {
1345+
return 0;
1346+
}
13361347
evidence->candidate_qn = target_node->qualified_name;
13371348
evidence->calls_emitted |=
13381349
emit_classified_edge(ctx, call, source_node, target_node, &res, module_qn, imp_keys,

‎src/pipeline/pass_definitions.c‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,10 +509,15 @@ static void process_def(cbm_pipeline_ctx_t *ctx, const CBMDefinition *def, const
509509
* (helpers.c): callables + type-like containers (INHERITS/IMPLEMENTS/method/
510510
* field resolution), Variable/Field (READS/WRITES resolution), and Table/View
511511
* (SQL FROM/JOIN lineage). pass_parallel.c and pipeline_incremental.c seed
512-
* through the same predicate, so the three registries cannot diverge. */
512+
* through the same predicate, so the three registries cannot diverge.
513+
* A generated member is registered by qualified name only, at all three. */
513514
if (node_id > 0 && cbm_label_is_registry_symbol(def->label)) {
514-
cbm_registry_add_lang_in_module(ctx->registry, def->name, def->qualified_name, def->label,
515-
lang, module_prefix_len);
515+
if (cbm_def_props_generated(def->extra_props)) {
516+
cbm_registry_add_qn_only(ctx->registry, def->qualified_name, def->label);
517+
} else {
518+
cbm_registry_add_lang_in_module(ctx->registry, def->name, def->qualified_name,
519+
def->label, lang, module_prefix_len);
520+
}
516521
}
517522
char *file_qn = cbm_pipeline_fqn_compute(ctx->project_name, rel, "__file__");
518523
const cbm_gbuf_node_t *file_node = cbm_gbuf_find_by_qn(ctx->gbuf, file_qn);

‎src/pipeline/pass_parallel.c‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,10 +1811,15 @@ static int register_and_link_def(cbm_pipeline_ctx_t *ctx, const CBMDefinition *d
18111811
return 0;
18121812
}
18131813
/* Registry membership is defined ONCE by cbm_label_is_registry_symbol
1814-
* (helpers.c) — see pass_definitions.c for the per-label rationale. */
1814+
* (helpers.c) — see pass_definitions.c for the per-label rationale, and
1815+
* for the generated member registered by qualified name only. */
18151816
if (cbm_label_is_registry_symbol(def->label)) {
1816-
cbm_registry_add_lang_in_module(ctx->registry, def->name, def->qualified_name, def->label,
1817-
lang, module_prefix_len);
1817+
if (cbm_def_props_generated(def->extra_props)) {
1818+
cbm_registry_add_qn_only(ctx->registry, def->qualified_name, def->label);
1819+
} else {
1820+
cbm_registry_add_lang_in_module(ctx->registry, def->name, def->qualified_name,
1821+
def->label, lang, module_prefix_len);
1822+
}
18181823
(*reg_entries)++;
18191824
}
18201825
char marked[CBM_SZ_4K];
@@ -3618,6 +3623,12 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
36183623
imp_keys, imp_vals, imp_count, route_mount, lang);
36193624
continue;
36203625
}
3626+
if (target_node && source_node->id != target_node->id &&
3627+
cbm_java_suppress_call_to_data_member(lang == CBM_LANG_JAVA, target_node->label)) {
3628+
/* Same guard as pass_calls.c — a Java call never targets a
3629+
* Variable or a Field. */
3630+
continue;
3631+
}
36213632
if (!target_node || source_node->id == target_node->id) {
36223633
/* HTTP/ASYNC calls to an EXTERNAL client library (`requests.get(url)`)
36233634
* resolve to an unindexed QN (target_node == NULL), but their edge

‎src/pipeline/pass_pkgmap.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2604,6 +2604,15 @@ static const cbm_gbuf_node_t *resolve_import_strategies(
26042604
if (import_candidate_is_python_local(ctx->gbuf, cand)) {
26052605
continue;
26062606
}
2607+
/* Nor is a generated member (a Java record's implicit
2608+
* accessor, an instance method no import can name). It is
2609+
* registered by qualified name only, and a package segment
2610+
* that merely spells it (`javax.crypto.spec` -> spec())
2611+
* must not bind the import -- or every use of the
2612+
* imported name -- to it. */
2613+
if (cbm_node_props_generated(cand->properties_json)) {
2614+
continue;
2615+
}
26072616
if (source_file_qn && cand->qualified_name &&
26082617
strcmp(cand->qualified_name, source_file_qn) == 0) {
26092618
continue; /* self */

‎src/pipeline/pipeline.h‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,24 @@ void cbm_registry_add_lang_in_module(cbm_registry_t *r, const char *name,
278278
const char *qualified_name, const char *label,
279279
CBMLanguage lang, size_t module_prefix_len);
280280

281+
/* Register a symbol for exact (qualified-name) lookups only: it is never
282+
* offered to the short-name strategies (unique_name, suffix_match,
283+
* field_type_hint, fuzzy). For a generated member (cbm_def_props_generated),
284+
* which the source never spells, so only a type-aware resolver that knows its
285+
* owner may bind it. The three graph registration sites route such a symbol
286+
* here instead of cbm_registry_add_lang_in_module. */
287+
void cbm_registry_add_qn_only(cbm_registry_t *r, const char *qualified_name, const char *label);
288+
289+
/* True when a definition's extra_props (key, value, ..., NULL) carry the
290+
* generated-member key (cbm.h CBM_PROP_GENERATED), e.g. a Java record's
291+
* implicit accessor. Pure; unit-tested in test_registry.c. */
292+
bool cbm_def_props_generated(const char *const *extra_props);
293+
294+
/* The stored-node twin of cbm_def_props_generated, for the incremental
295+
* registry seed: true when a node's properties JSON carries that key as a
296+
* string property. Pure; unit-tested in test_registry.c. */
297+
bool cbm_node_props_generated(const char *properties_json);
298+
281299
/* True when code in `caller` can call a symbol defined in `target` by bare
282300
* name: same language, same interop family (JS/TS + script hosts, C/C++/CUDA/
283301
* ObjC, JVM, .NET, ...; the table lives in registry.c), or Swift with the C
@@ -512,6 +530,13 @@ bool cbm_c_member_rule_file(CBMLanguage lang, const char *rel_path);
512530
* a member); NULL when none or several do. */
513531
const char *cbm_registry_unique_field_qn(const cbm_registry_t *r, const char *member_name);
514532

533+
/* A Java call expression never names data: `p.x()` is a method invocation
534+
* whatever `x` spells elsewhere in the project. Drops a CALLS bind whose
535+
* target is a Variable or Field. Java only — Kotlin properties and C function
536+
* pointers are callable names that are not methods. Pure; unit-tested in
537+
* test_registry.c. */
538+
bool cbm_java_suppress_call_to_data_member(bool is_java, const char *target_label);
539+
515540
/* Get the label of a qualified name, or NULL if not found. */
516541
const char *cbm_registry_label_of(const cbm_registry_t *r, const char *qn);
517542

0 commit comments

Comments
 (0)