MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / eval_conditional

Function eval_conditional

internal/cbm/lsp/ts_lsp.c:1315–1358  ·  view source on GitHub ↗

Evaluate a CBM_TYPE_CONDITIONAL `T extends U ? X : Y`. With distribution: when T is a UNION, evaluate per member and union the results. Uses the partial relater for the extends check.

Source from the content-addressed store, hash-verified

1313// a UNION, evaluate per member and union the results. Uses the partial relater for
1314// the extends check.
1315static const CBMType *eval_conditional(TSLSPContext *ctx, const CBMType *t, int depth) {
1316 if (!t || t->kind != CBM_TYPE_CONDITIONAL || depth > 16)
1317 return t;
1318 const CBMType *check = t->data.conditional.check;
1319 const CBMType *extends = t->data.conditional.extends;
1320 const CBMType *tb = t->data.conditional.true_branch;
1321 const CBMType *fb = t->data.conditional.false_branch;
1322 if (!check || !extends)
1323 return t;
1324
1325 // Distribution: when check is a UNION, evaluate per member and union results.
1326 if (check->kind == CBM_TYPE_UNION && check->data.union_type.members &&
1327 check->data.union_type.count > 0) {
1328 const CBMType **results = (const CBMType **)cbm_arena_alloc(
1329 ctx->arena, (size_t)(check->data.union_type.count + 1) * sizeof(const CBMType *));
1330 if (!results)
1331 return t;
1332 int n = 0;
1333 for (int i = 0; i < check->data.union_type.count; i++) {
1334 const CBMType *member = check->data.union_type.members[i];
1335 const CBMType *sub = cbm_type_conditional(ctx->arena, member, extends, tb, fb);
1336 results[n++] = eval_conditional(ctx, sub, depth + 1);
1337 }
1338 results[n] = NULL;
1339 if (n == 1)
1340 return results[0];
1341 return cbm_type_union(ctx->arena, results, n);
1342 }
1343
1344 // `infer X` in extends pattern: pattern-match check against extends, capturing
1345 // bindings, then substitute them in the true_branch.
1346 if (contains_infer(extends, 0)) {
1347 TSInferBinding binds[TS_INFER_MAX] = {{0}};
1348 int bind_count = 0;
1349 if (match_with_infer(ctx, check, extends, binds, &bind_count, 0)) {
1350 return subst_infer_bindings(ctx, tb, binds, bind_count, 0);
1351 }
1352 return fb;
1353 }
1354
1355 // Non-distributive: single resolution.
1356 bool yes = ts_is_assignable(ctx, check, extends);
1357 return yes ? tb : fb;
1358}
1359
1360// Resolve a CBMType against the registry's alias chain when it's a NAMED type that's a
1361// pure alias (`type Foo = Bar`). Also evaluates inline conditional types.

Callers 1

simplify_typeFunction · 0.85

Calls 7

cbm_type_conditionalFunction · 0.85
cbm_type_unionFunction · 0.85
contains_inferFunction · 0.85
match_with_inferFunction · 0.85
subst_infer_bindingsFunction · 0.85
ts_is_assignableFunction · 0.85
cbm_arena_allocFunction · 0.50

Tested by

no test coverage detected