Skip to content

Commit d8a845e

Browse files
committed
fix(swift): preserve overload identity and label-aware call edges
Enable typed, labeled Swift callable identities on the shared DeusData#2061 plumbing. Resolve calls by compatible labels, defaulted parameters, and trailing closures; retain all compatible overload edges with candidate counts. Cover registry, pipeline, and signature behavior. Refs DeusData#2061 Signed-off-by: DavidHLP <[email protected]>
1 parent 7abaa91 commit d8a845e

16 files changed

Lines changed: 680 additions & 11 deletions

‎internal/cbm/callable_sig.c‎

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ static const uint64_t SIG_FNV_PRIME = 0x100000001b3ULL;
3232
static const char SIG_OPERATOR_CHARS[] = "+-*/%^&|~!=<>?[]";
3333

3434
CBMCallableIdentity cbm_callable_identity(CBMLanguage lang) {
35-
/* Every language keeps its historical QN until its enable change lands
36-
* (with the index-format bump that change carries). The planned modes:
37-
* Java/Kotlin/C#/C++/CUDA/Scala TYPED, Swift LABELED_TYPED, ObjC LABELED,
38-
* dynamic tier-2 languages ARITY. */
39-
(void)lang;
35+
/* Each language enables identity with its own index-format bump. */
36+
if (lang == CBM_LANG_SWIFT) {
37+
return CBM_CALLABLE_ID_LABELED_TYPED;
38+
}
4039
return CBM_CALLABLE_ID_NONE;
4140
}
4241

@@ -737,6 +736,11 @@ static void sig_swift_param(sig_ctx_t *c, TSNode p) {
737736
if (ts_node_eq(ch, internal)) {
738737
continue;
739738
}
739+
/* A default expression selects call arity, not type identity. */
740+
if ((field && strcmp(field, "default_value") == 0) ||
741+
sig_node_text_is(c, ch, "=")) {
742+
break;
743+
}
740744
const char *k = ts_node_type(ch);
741745
if (strcmp(k, "parameter_modifiers") == 0) {
742746
uint32_t mc = ts_node_named_child_count(ch);
@@ -770,6 +774,42 @@ static void sig_swift_params(sig_ctx_t *c, TSNode node) {
770774
}
771775
}
772776

777+
uint64_t cbm_swift_default_mask(TSNode node, const char *source, uint8_t *count) {
778+
(void)source;
779+
uint64_t defaults = 0;
780+
unsigned parameters = 0;
781+
int last = -1;
782+
uint32_t children = ts_node_child_count(node);
783+
for (uint32_t i = 0; i < children; i++) {
784+
TSNode ch = ts_node_child(node, i);
785+
const char *kind = ts_node_type(ch);
786+
if (strcmp(kind, "parameter") == 0) {
787+
last = (int)parameters++;
788+
if (last < 64) {
789+
uint32_t pc = ts_node_child_count(ch);
790+
for (uint32_t j = 0; j < pc; j++) {
791+
TSNode part = ts_node_child(ch, j);
792+
const char *pk = ts_node_type(part);
793+
if (strcmp(pk, "=") == 0 || strstr(pk, "default_value") != NULL) {
794+
defaults |= UINT64_C(1) << last;
795+
break;
796+
}
797+
}
798+
}
799+
} else if (last >= 0 && last < 64 && strcmp(kind, "=") == 0) {
800+
defaults |= UINT64_C(1) << last;
801+
} else if (last >= 0 && (strcmp(kind, ")") == 0 || strcmp(kind, "function_body") == 0)) {
802+
break;
803+
}
804+
}
805+
if (count) {
806+
/* ponytail: >64 parameters have no bitmask; use a dynamic mask if Swift
807+
* code with that many parameters needs overload call resolution. */
808+
*count = parameters > 64 ? UINT8_MAX : (uint8_t)parameters;
809+
}
810+
return defaults;
811+
}
812+
773813
/* ── Scala ─────────────────────────────────────────────────────── */
774814

775815
static void sig_scala_params(sig_ctx_t *c, TSNode node) {

‎internal/cbm/callable_sig.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "arena.h"
3939
#include "tree_sitter/api.h"
4040
#include <stddef.h>
41+
#include <stdint.h>
4142

4243
typedef enum {
4344
CBM_CALLABLE_ID_NONE = 0, /* historical base QN, no suffix */
@@ -56,6 +57,10 @@ enum { CBM_CALLABLE_SIG_MAX = 200 };
5657
* spell the new member under -Wmissing-field-initializers. */
5758
CBMCallableIdentity cbm_callable_identity(CBMLanguage lang);
5859

60+
/* Swift parameter defaults are call-resolution metadata, not graph identity.
61+
* Returns a bit per parameter; count > 64 is reported as 255. */
62+
uint64_t cbm_swift_default_mask(TSNode func_node, const char *source, uint8_t *count);
63+
5964
/* The identity suffix for the callable at `func_node` in the language's own
6065
* mode, or NULL when that mode is NONE (or the node carries no parameter
6166
* information the mode can use). Arena-owned. */

‎internal/cbm/cbm.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ typedef struct {
242242
* qualified_name (base QN = the first qn_sig_off bytes); 0 = no suffix.
243243
* Always 0 until a language enables its callable_identity mode. */
244244
uint32_t qn_sig_off;
245+
/* Swift only: defaulted parameters, indexed in declaration order. */
246+
uint64_t swift_default_mask;
247+
uint8_t swift_param_count;
245248
} CBMDefinition;
246249

247250
/* Argument captured from a call expression */
@@ -298,6 +301,8 @@ typedef struct {
298301
// (self.compiler.apply_converters()). An object the
299302
// class owns, not a parameter: read by the weak-member
300303
// guard's unique-name exemption. Default false.
304+
bool swift_trailing_closure;
305+
bool swift_args_truncated;
301306
} CBMCall;
302307

303308
typedef struct {

‎internal/cbm/extract_calls.c‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,6 +2061,8 @@ static void process_keyword_arg(CBMExtractCtx *ctx, TSNode arg_node, CBMCallArg
20612061
}
20622062
}
20632063

2064+
static TSNode swift_argument_value(TSNode arg);
2065+
20642066
/* Extract all arguments from a call expression into call->args[]. */
20652067
static void extract_call_args(CBMExtractCtx *ctx, TSNode args, CBMCall *call) {
20662068
uint32_t argc = ts_node_named_child_count(args);
@@ -2085,6 +2087,26 @@ static void extract_call_args(CBMExtractCtx *ctx, TSNode args, CBMCall *call) {
20852087
CBMCallArg *ca = &call->args[call->arg_count];
20862088
memset(ca, 0, sizeof(*ca));
20872089

2090+
if (ctx->language == CBM_LANG_SWIFT && strcmp(ak, "value_argument") == 0) {
2091+
TSNode first = ts_node_named_child(arg_node, 0);
2092+
if (!ts_node_is_null(first) &&
2093+
strcmp(ts_node_type(first), "value_argument_label") == 0) {
2094+
TSNode label = ts_node_named_child(first, 0);
2095+
if (ts_node_is_null(label)) {
2096+
label = first;
2097+
}
2098+
ca->keyword = cbm_node_text(ctx->arena, label, ctx->source);
2099+
if (ca->keyword) {
2100+
size_t n = strlen(ca->keyword);
2101+
if (n > 0 && ca->keyword[n - 1] == ':') {
2102+
ca->keyword = cbm_arena_strndup(ctx->arena, ca->keyword, n - 1);
2103+
}
2104+
}
2105+
}
2106+
arg_node = swift_argument_value(arg_node);
2107+
ak = ts_node_type(arg_node);
2108+
}
2109+
20882110
if (strcmp(ak, "keyword_argument") == 0 || strcmp(ak, "pair") == 0) {
20892111
process_keyword_arg(ctx, arg_node, ca);
20902112
ca->index = positional_idx++;
@@ -3831,6 +3853,17 @@ CBMInvocationDescriptor handle_calls(CBMExtractCtx *ctx, TSNode node, const CBML
38313853
if (ts_node_is_null(args) && ctx->language == CBM_LANG_SWIFT) {
38323854
args = swift_call_args(node);
38333855
}
3856+
if (ctx->language == CBM_LANG_SWIFT) {
3857+
TSNode suffix = cbm_find_child_by_kind(node, "call_suffix");
3858+
if (!ts_node_is_null(suffix)) {
3859+
call.swift_trailing_closure =
3860+
!ts_node_is_null(cbm_find_child_by_kind(suffix, "lambda_literal"));
3861+
}
3862+
if (!ts_node_is_null(args) &&
3863+
ts_node_named_child_count(args) > CBM_MAX_CALL_ARGS) {
3864+
call.swift_args_truncated = true;
3865+
}
3866+
}
38343867
if (!ts_node_is_null(args)) {
38353868
call.first_string_arg = extract_url_or_topic_arg(ctx, args);
38363869
/* #952: routes registered inside Laravel `prefix()->group()`

‎internal/cbm/extract_defs.c‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "cbm.h"
2+
#include "callable_sig.h"
23
#include "arena.h" // CBMArena, cbm_arena_alloc/strdup/sprintf
34
#include "helpers.h"
45
#include "lang_specs.h"
@@ -3944,6 +3945,20 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
39443945
def.is_test = rust_def_is_test(def.decorators);
39453946
}
39463947

3948+
if (ctx->language == CBM_LANG_SWIFT) {
3949+
const char *sig = cbm_callable_sig(a, func_node, ctx->source, ctx->language);
3950+
if (!sig) {
3951+
return;
3952+
}
3953+
def.qn_sig_off = (uint32_t)strlen(def.qualified_name);
3954+
def.qualified_name = cbm_arena_sprintf(a, "%s%s", def.qualified_name, sig);
3955+
if (!def.qualified_name) {
3956+
return;
3957+
}
3958+
def.swift_default_mask =
3959+
cbm_swift_default_mask(func_node, ctx->source, &def.swift_param_count);
3960+
}
3961+
39473962
// C++/CUDA: GoogleTest macros are test functions (#1266).
39483963
if (is_gtest) {
39493964
def.is_test = true;
@@ -5091,6 +5106,18 @@ static void push_method_def(CBMExtractCtx *ctx, TSNode child, TSNode class_node,
50915106
memset(&def, 0, sizeof(def));
50925107
def.name = name;
50935108
def.qualified_name = method_qn;
5109+
if (ctx->language == CBM_LANG_SWIFT) {
5110+
const char *sig = cbm_callable_sig(a, child, ctx->source, ctx->language);
5111+
if (!sig) {
5112+
return;
5113+
}
5114+
def.qn_sig_off = (uint32_t)strlen(method_qn);
5115+
def.qualified_name = cbm_arena_sprintf(a, "%s%s", method_qn, sig);
5116+
if (!def.qualified_name) {
5117+
return;
5118+
}
5119+
def.swift_default_mask = cbm_swift_default_mask(child, ctx->source, &def.swift_param_count);
5120+
}
50945121
def.label = "Method";
50955122
def.file_path = ctx->rel_path;
50965123
def.parent_class = class_qn;

‎internal/cbm/extract_unified.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "extract_unified.h"
22
#include "arena.h" // cbm_arena_sprintf
33
#include "cbm.h" // CBMExtractCtx
4+
#include "callable_sig.h"
45
#include "helpers.h"
56
#include "lang_specs.h" // CBMLangSpec, cbm_lang_spec, CBM_LANG_*
67
#include "tree_sitter/api.h" // TSNode, TSTreeCursor, ts_tree_cursor_*, ts_node_*
@@ -1092,6 +1093,16 @@ static const char *compute_func_qn(CBMExtractCtx *ctx, TSNode node, const CBMLan
10921093
* an in-body call sources to a QN one or more segments short of the def, and
10931094
* the edge is dropped at write. */
10941095
const char *qn_name = name;
1096+
if (ctx->language == CBM_LANG_SWIFT) {
1097+
const char *sig = cbm_callable_sig(ctx->arena, node, ctx->source, ctx->language);
1098+
if (!sig) {
1099+
return NULL;
1100+
}
1101+
qn_name = cbm_arena_sprintf(ctx->arena, "%s%s", name, sig);
1102+
if (!qn_name) {
1103+
return NULL;
1104+
}
1105+
}
10951106
if (ctx->language == CBM_LANG_NIX) {
10961107
qn_name = cbm_nix_qn_name(ctx->arena, node, ctx->source, name);
10971108
if (!qn_name || !qn_name[0]) {

‎src/pipeline/pass_calls.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,28 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call,
544544
}
545545
}
546546

547+
if (lang == CBM_LANG_SWIFT) {
548+
const char *candidates[CBM_SZ_256];
549+
int count = cbm_registry_swift_candidates(ctx->registry, call, module_qn, imp_vals,
550+
imp_count, candidates, CBM_SZ_256);
551+
if (count != 0) {
552+
int emitted = 0;
553+
for (int i = 0; i < count; i++) {
554+
const cbm_gbuf_node_t *target = cbm_gbuf_find_by_qn(ctx->gbuf, candidates[i]);
555+
if (target && target->id != source_node->id) {
556+
cbm_resolution_t selected = {.qualified_name = candidates[i],
557+
.strategy = "swift_labels",
558+
.confidence = count == 1 ? 0.90 : 0.55,
559+
.candidate_count = count};
560+
emit_classified_edge(ctx, call, source_node, target, &selected, module_qn,
561+
imp_keys, imp_vals, imp_count, false);
562+
emitted++;
563+
}
564+
}
565+
return emitted > 0 ? SKIP_ONE : 0;
566+
}
567+
}
568+
547569
cbm_resolution_t res = cbm_registry_resolve(ctx->registry, call->callee_name, module_qn,
548570
imp_keys, imp_vals, imp_count);
549571
if (!res.qualified_name || res.qualified_name[0] == '\0') {

‎src/pipeline/pass_definitions.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,12 @@ static void build_def_props(char *buf, size_t bufsize, const CBMDefinition *def)
284284
return;
285285
}
286286
size_t pos = (size_t)n;
287+
if (def->qn_sig_off && bufsize - pos > 80) {
288+
pos += (size_t)snprintf(buf + pos, bufsize - pos,
289+
",\"swift_defaults\":\"%016llx\",\"swift_params\":%u",
290+
(unsigned long long)def->swift_default_mask,
291+
(unsigned)def->swift_param_count);
292+
}
287293
append_json_string(buf, bufsize, &pos, "docstring", def->docstring);
288294
append_json_string(buf, bufsize, &pos, "signature", def->signature);
289295
append_json_string(buf, bufsize, &pos, "return_type", def->return_type);
@@ -336,6 +342,10 @@ static void process_def(cbm_pipeline_ctx_t *ctx, const CBMDefinition *def, const
336342
* through the same predicate, so the three registries cannot diverge. */
337343
if (node_id > 0 && cbm_label_is_registry_symbol(def->label)) {
338344
cbm_registry_add(ctx->registry, def->name, def->qualified_name, def->label);
345+
if (def->qn_sig_off) {
346+
cbm_registry_set_swift_signature(ctx->registry, def->qualified_name,
347+
def->swift_default_mask, def->swift_param_count);
348+
}
339349
}
340350
char *file_qn = cbm_pipeline_fqn_compute(ctx->project_name, rel, "__file__");
341351
const cbm_gbuf_node_t *file_node = cbm_gbuf_find_by_qn(ctx->gbuf, file_qn);

‎src/pipeline/pass_parallel.c‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,12 @@ static void build_def_props(char *buf, size_t bufsize, const CBMDefinition *def)
512512
return;
513513
}
514514
size_t pos = (size_t)n;
515+
if (def->qn_sig_off && bufsize - pos > 80) {
516+
pos += (size_t)snprintf(buf + pos, bufsize - pos,
517+
",\"swift_defaults\":\"%016llx\",\"swift_params\":%u",
518+
(unsigned long long)def->swift_default_mask,
519+
(unsigned)def->swift_param_count);
520+
}
515521
append_json_string(buf, bufsize, &pos, "docstring", def->docstring);
516522
append_json_string(buf, bufsize, &pos, "signature", def->signature);
517523
append_json_string(buf, bufsize, &pos, "return_type", def->return_type);
@@ -1589,6 +1595,10 @@ static int register_and_link_def(cbm_pipeline_ctx_t *ctx, const CBMDefinition *d
15891595
* (helpers.c) — see pass_definitions.c for the per-label rationale. */
15901596
if (cbm_label_is_registry_symbol(def->label)) {
15911597
cbm_registry_add(ctx->registry, def->name, def->qualified_name, def->label);
1598+
if (def->qn_sig_off) {
1599+
cbm_registry_set_swift_signature(ctx->registry, def->qualified_name,
1600+
def->swift_default_mask, def->swift_param_count);
1601+
}
15921602
(*reg_entries)++;
15931603
}
15941604
const cbm_gbuf_node_t *def_node = cbm_gbuf_find_by_qn(ctx->gbuf, def->qualified_name);
@@ -2984,6 +2994,33 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
29842994
}
29852995
}
29862996

2997+
if (lang == CBM_LANG_SWIFT && !lsp_target) {
2998+
const char *candidates[CBM_SZ_256];
2999+
int count = cbm_registry_swift_candidates(rc->registry, call, module_qn, imp_vals,
3000+
imp_count, candidates, CBM_SZ_256);
3001+
if (count > 0) {
3002+
for (int i = 0; i < count; i++) {
3003+
const cbm_gbuf_node_t *target = cbm_gbuf_find_by_qn(rc->main_gbuf,
3004+
candidates[i]);
3005+
if (!target || target->id == source_node->id) {
3006+
continue;
3007+
}
3008+
cbm_resolution_t selected = {.qualified_name = candidates[i],
3009+
.strategy = "swift_labels",
3010+
.confidence = count == 1 ? 0.90 : 0.55,
3011+
.candidate_count = count};
3012+
emit_service_edge(ws->local_edge_buf, source_node, target, call, &selected,
3013+
module_qn, rc->registry, rc->main_gbuf, imp_keys, imp_vals,
3014+
imp_count, false);
3015+
ws->calls_resolved++;
3016+
}
3017+
continue;
3018+
}
3019+
if (count < 0) {
3020+
res = (cbm_resolution_t){0};
3021+
}
3022+
}
3023+
29873024
if (!res.qualified_name || res.qualified_name[0] == '\0') {
29883025
if (cbm_service_pattern_route_method(call->callee_name) != NULL) {
29893026
cbm_resolution_t fake_res = {.qualified_name = call->callee_name,

‎src/pipeline/pipeline.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <stdatomic.h>
2121

2222
#include "discover/discover.h" /* cbm_ignored_file_t (#963) */
23+
#include "cbm.h" /* CBMCall for Swift overload selection */
2324
#include "foundation/constants.h" /* CBM_SZ_512 */
2425

2526
/* Forward declarations */
@@ -225,6 +226,15 @@ void cbm_registry_free(cbm_registry_t *r);
225226
void cbm_registry_add(cbm_registry_t *r, const char *name, const char *qualified_name,
226227
const char *label);
227228

229+
/* Swift-only metadata and label-compatible overload selection. The returned
230+
* QNs are borrowed from the registry and all belong to one base callable.
231+
* -1 means this name has Swift definitions but no compatible overload. */
232+
void cbm_registry_set_swift_signature(cbm_registry_t *r, const char *qualified_name,
233+
uint64_t default_mask, uint8_t param_count);
234+
int cbm_registry_swift_candidates(const cbm_registry_t *r, const CBMCall *call,
235+
const char *module_qn, const char **import_vals,
236+
int import_count, const char **out, int out_cap);
237+
228238
/* Resolve a callee name using prioritized strategies.
229239
* import_map: NULL-terminated array of {local_name, resolved_qn} pairs, or NULL.
230240
* Returns result with qualified_name="" if unresolved.

0 commit comments

Comments
 (0)