Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

feat(lsp): type-rep extensions for Python (UNION, LITERAL, PROTOCOL, MODULE, CALLABLE)

Martin Vogeldef7b94db7f4
diff --git a/Makefile.cbm b/Makefile.cbm
index a7cb729c5..c7270c99e 100644
--- a/Makefile.cbm
+++ b/Makefile.cbm
@@ -311,6 +311,8 @@ TEST_C_LSP_SRCS = tests/test_c_lsp.c
TEST_SCOPE_SRCS = tests/test_scope.c
+TEST_TYPE_REP_SRCS = tests/test_type_rep.c
+
TEST_INTEGRATION_SRCS = tests/test_integration.c tests/test_incremental.c
TEST_TRACES_SRCS = tests/test_traces.c
@@ -328,7 +330,7 @@ TEST_YAML_SRCS = tests/test_yaml.c
TEST_SIMHASH_SRCS = tests/test_simhash.c
-ALL_TEST_SRCS = $(TEST_FOUNDATION_SRCS) $(TEST_EXTRACTION_SRCS) $(TEST_STORE_SRCS) $(TEST_CYPHER_SRCS) $(TEST_MCP_SRCS) $(TEST_DISCOVER_SRCS) $(TEST_GRAPH_BUFFER_SRCS) $(TEST_PIPELINE_SRCS) $(TEST_WATCHER_SRCS) $(TEST_LZ4_SRCS) $(TEST_ZSTD_SRCS) $(TEST_ARTIFACT_SRCS) $(TEST_SQLITE_WRITER_SRCS) $(TEST_GO_LSP_SRCS) $(TEST_C_LSP_SRCS) $(TEST_SCOPE_SRCS) $(TEST_TRACES_SRCS) $(TEST_CLI_SRCS) $(TEST_MEM_SRCS) $(TEST_UI_SRCS) $(TEST_SECURITY_SRCS) $(TEST_YAML_SRCS) $(TEST_SIMHASH_SRCS) $(TEST_INTEGRATION_SRCS)
+ALL_TEST_SRCS = $(TEST_FOUNDATION_SRCS) $(TEST_EXTRACTION_SRCS) $(TEST_STORE_SRCS) $(TEST_CYPHER_SRCS) $(TEST_MCP_SRCS) $(TEST_DISCOVER_SRCS) $(TEST_GRAPH_BUFFER_SRCS) $(TEST_PIPELINE_SRCS) $(TEST_WATCHER_SRCS) $(TEST_LZ4_SRCS) $(TEST_ZSTD_SRCS) $(TEST_ARTIFACT_SRCS) $(TEST_SQLITE_WRITER_SRCS) $(TEST_GO_LSP_SRCS) $(TEST_C_LSP_SRCS) $(TEST_SCOPE_SRCS) $(TEST_TYPE_REP_SRCS) $(TEST_TRACES_SRCS) $(TEST_CLI_SRCS) $(TEST_MEM_SRCS) $(TEST_UI_SRCS) $(TEST_SECURITY_SRCS) $(TEST_YAML_SRCS) $(TEST_SIMHASH_SRCS) $(TEST_INTEGRATION_SRCS)
# ── Build directories ────────────────────────────────────────────
diff --git a/internal/cbm/lsp/type_rep.c b/internal/cbm/lsp/type_rep.c
index 2dfee7ce8..802e7cef0 100644
--- a/internal/cbm/lsp/type_rep.c
+++ b/internal/cbm/lsp/type_rep.c
@@ -176,6 +176,139 @@ const CBMType* cbm_type_alias(CBMArena* a, const char* alias_qn, const CBMType*
return t;
}
+// --- Python-flavored constructors -------------------------------------------
+
+// Dedupe members by structural equality, in place. Returns new length.
+// Preserves first-seen order so output is deterministic.
+static int union_member_dedupe(const CBMType** scratch, int count) {
+ int out = 0;
+ for (int i = 0; i < count; i++) {
+ bool seen = false;
+ for (int j = 0; j < out; j++) {
+ if (cbm_type_equal(scratch[i], scratch[j])) {
+ seen = true;
+ break;
+ }
+ }
+ if (!seen) {
+ scratch[out++] = scratch[i];
+ }
+ }
+ return out;
+}
+
+const CBMType* cbm_type_union(CBMArena* a, const CBMType** members, int count) {
+ if (!members || count <= 0) return &unknown_singleton;
+
+ // Flatten: nested UNIONs unfold their members into the parent.
+ int flat_cap = count * 2 + 4;
+ const CBMType** flat = (const CBMType**)cbm_arena_alloc(a, flat_cap * sizeof(const CBMType*));
+ if (!flat) return &unknown_singleton;
+ int flat_count = 0;
+ for (int i = 0; i < count; i++) {
+ const CBMType* m = members[i];
+ if (!m || cbm_type_is_unknown(m)) continue;
+ if (m->kind == CBM_TYPE_UNION) {
+ for (int j = 0; j < m->data.union_type.count; j++) {
+ if (flat_count < flat_cap) flat[flat_count++] = m->data.union_type.members[j];
+ }
+ } else {
+ if (flat_count < flat_cap) flat[flat_count++] = m;
+ }
+ }
+ if (flat_count == 0) return &unknown_singleton;
+
+ // Dedupe by structural equality.
+ int unique_count = union_member_dedupe(flat, flat_count);
+ if (unique_count == 1) return flat[0];
+
+ CBMType* t = (CBMType*)cbm_arena_alloc(a, sizeof(CBMType));
+ if (!t) return &unknown_singleton;
+ memset(t, 0, sizeof(CBMType));
+ t->kind = CBM_TYPE_UNION;
+ const CBMType** arr = (const CBMType**)cbm_arena_alloc(a, (unique_count + 1) * sizeof(const CBMType*));
+ if (!arr) return &unknown_singleton;
+ for (int i = 0; i < unique_count; i++) arr[i] = flat[i];
+ arr[unique_count] = NULL;
+ t->data.union_type.members = arr;
+ t->data.union_type.count = unique_count;
+ return t;
+}
+
+const CBMType* cbm_type_optional(CBMArena* a, const CBMType* inner) {
+ if (!inner) return &unknown_singleton;
+ const CBMType* none_t = cbm_type_builtin(a, "None");
+ const CBMType* members[2] = { inner, none_t };
+ return cbm_type_union(a, members, 2);
+}
+
+const CBMType* cbm_type_literal(CBMArena* a, const CBMType* base, const char* literal_text) {
+ CBMType* t = (CBMType*)cbm_arena_alloc(a, sizeof(CBMType));
+ if (!t) return &unknown_singleton;
+ memset(t, 0, sizeof(CBMType));
+ t->kind = CBM_TYPE_LITERAL;
+ t->data.literal.base = base ? base : &unknown_singleton;
+ t->data.literal.literal_text = literal_text ? cbm_arena_strdup(a, literal_text) : NULL;
+ return t;
+}
+
+const CBMType* cbm_type_protocol(CBMArena* a, const char* qualified_name,
+ const char** method_names, const CBMType** method_sigs) {
+ CBMType* t = (CBMType*)cbm_arena_alloc(a, sizeof(CBMType));
+ if (!t) return &unknown_singleton;
+ memset(t, 0, sizeof(CBMType));
+ t->kind = CBM_TYPE_PROTOCOL;
+ t->data.protocol.qualified_name = qualified_name ? cbm_arena_strdup(a, qualified_name) : NULL;
+
+ int n = 0;
+ if (method_names) {
+ while (method_names[n]) n++;
+ }
+ if (n > 0) {
+ const char** names = (const char**)cbm_arena_alloc(a, (n + 1) * sizeof(const char*));
+ const CBMType** sigs = (const CBMType**)cbm_arena_alloc(a, (n + 1) * sizeof(const CBMType*));
+ if (names && sigs) {
+ for (int i = 0; i < n; i++) {
+ names[i] = cbm_arena_strdup(a, method_names[i]);
+ sigs[i] = method_sigs ? method_sigs[i] : NULL;
+ }
+ names[n] = NULL;
+ sigs[n] = NULL;
+ t->data.protocol.method_names = names;
+ t->data.protocol.method_sigs = sigs;
+ }
+ }
+ return t;
+}
+
+const CBMType* cbm_type_module(CBMArena* a, const char* module_qn) {
+ CBMType* t = (CBMType*)cbm_arena_alloc(a, sizeof(CBMType));
+ if (!t) return &unknown_singleton;
+ memset(t, 0, sizeof(CBMType));
+ t->kind = CBM_TYPE_MODULE;
+ t->data.module.module_qn = module_qn ? cbm_arena_strdup(a, module_qn) : NULL;
+ return t;
+}
+
+const CBMType* cbm_type_callable(CBMArena* a, const CBMType** param_types, int param_count,
+ const CBMType* return_type) {
+ CBMType* t = (CBMType*)cbm_arena_alloc(a, sizeof(CBMType));
+ if (!t) return &unknown_singleton;
+ memset(t, 0, sizeof(CBMType));
+ t->kind = CBM_TYPE_CALLABLE;
+ t->data.callable.param_count = param_count;
+ t->data.callable.return_type = return_type ? return_type : &unknown_singleton;
+ if (param_count > 0 && param_types) {
+ const CBMType** arr = (const CBMType**)cbm_arena_alloc(a, (param_count + 1) * sizeof(const CBMType*));
+ if (arr) {
+ for (int i = 0; i < param_count; i++) arr[i] = param_types[i];
+ arr[param_count] = NULL;
+ t->data.callable.param_types = arr;
+ }
+ }
+ return t;
+}
+
// Operations
const CBMType* cbm_type_deref(const CBMType* t) {
@@ -219,6 +352,142 @@ bool cbm_type_is_reference(const CBMType* t) {
return t && (t->kind == CBM_TYPE_REFERENCE || t->kind == CBM_TYPE_RVALUE_REF);
}
+bool cbm_type_is_union(const CBMType* t) {
+ return t && t->kind == CBM_TYPE_UNION;
+}
+
+bool cbm_type_is_protocol(const CBMType* t) {
+ return t && t->kind == CBM_TYPE_PROTOCOL;
+}
+
+bool cbm_type_is_module(const CBMType* t) {
+ return t && t->kind == CBM_TYPE_MODULE;
+}
+
+static bool str_eq_or_both_null(const char* a, const char* b) {
+ if (a == b) return true;
+ if (!a || !b) return false;
+ return strcmp(a, b) == 0;
+}
+
+bool cbm_type_equal(const CBMType* a, const CBMType* b) {
+ if (a == b) return true;
+ if (!a || !b) return false;
+ if (a->kind != b->kind) return false;
+
+ switch (a->kind) {
+ case CBM_TYPE_UNKNOWN:
+ return true;
+ case CBM_TYPE_NAMED:
+ return str_eq_or_both_null(a->data.named.qualified_name, b->data.named.qualified_name);
+ case CBM_TYPE_BUILTIN:
+ return str_eq_or_both_null(a->data.builtin.name, b->data.builtin.name);
+ case CBM_TYPE_TYPE_PARAM:
+ return str_eq_or_both_null(a->data.type_param.name, b->data.type_param.name);
+ case CBM_TYPE_POINTER:
+ return cbm_type_equal(a->data.pointer.elem, b->data.pointer.elem);
+ case CBM_TYPE_SLICE:
+ return cbm_type_equal(a->data.slice.elem, b->data.slice.elem);
+ case CBM_TYPE_REFERENCE:
+ case CBM_TYPE_RVALUE_REF:
+ return cbm_type_equal(a->data.reference.elem, b->data.reference.elem);
+ case CBM_TYPE_MAP:
+ return cbm_type_equal(a->data.map.key, b->data.map.key)
+ && cbm_type_equal(a->data.map.value, b->data.map.value);
+ case CBM_TYPE_CHANNEL:
+ return a->data.channel.direction == b->data.channel.direction
+ && cbm_type_equal(a->data.channel.elem, b->data.channel.elem);
+ case CBM_TYPE_TUPLE: {
+ if (a->data.tuple.count != b->data.tuple.count) return false;
+ for (int i = 0; i < a->data.tuple.count; i++) {
+ if (!cbm_type_equal(a->data.tuple.elems[i], b->data.tuple.elems[i])) return false;
+ }
+ return true;
+ }
+ case CBM_TYPE_TEMPLATE: {
+ if (!str_eq_or_both_null(a->data.template_type.template_name,
+ b->data.template_type.template_name)) return false;
+ if (a->data.template_type.arg_count != b->data.template_type.arg_count) return false;
+ for (int i = 0; i < a->data.template_type.arg_count; i++) {
+ if (!cbm_type_equal(a->data.template_type.template_args[i],
+ b->data.template_type.template_args[i])) return false;
+ }
+ return true;
+ }
+ case CBM_TYPE_ALIAS:
+ return str_eq_or_both_null(a->data.alias.alias_qn, b->data.alias.alias_qn);
+ case CBM_TYPE_UNION: {
+ if (a->data.union_type.count != b->data.union_type.count) return false;
+ // Order-independent: every a-member must appear in b's set.
+ for (int i = 0; i < a->data.union_type.count; i++) {
+ bool found = false;
+ for (int j = 0; j < b->data.union_type.count; j++) {
+ if (cbm_type_equal(a->data.union_type.members[i],
+ b->data.union_type.members[j])) {
+ found = true;
+ break;
+ }
+ }
+ if (!found) return false;
+ }
+ return true;
+ }
+ case CBM_TYPE_LITERAL:
+ return cbm_type_equal(a->data.literal.base, b->data.literal.base)
+ && str_eq_or_both_null(a->data.literal.literal_text, b->data.literal.literal_text);
+ case CBM_TYPE_PROTOCOL:
+ return str_eq_or_both_null(a->data.protocol.qualified_name,
+ b->data.protocol.qualified_name);
+ case CBM_TYPE_MODULE:
+ return str_eq_or_both_null(a->data.module.module_qn, b->data.module.module_qn);
+ case CBM_TYPE_CALLABLE: {
+ if (a->data.callable.param_count != b->data.callable.param_count) return false;
+ if (!cbm_type_equal(a->data.callable.return_type, b->data.callable.return_type)) return false;
+ if (a->data.callable.param_count > 0) {
+ for (int i = 0; i < a->data.callable.param_count; i++) {
+ if (!cbm_type_equal(a->data.callable.param_types[i],
+ b->data.callable.param_types[i])) return false;
+ }
+ }
+ return true;
+ }
+ case CBM_TYPE_FUNC:
+ case CBM_TYPE_INTERFACE:
+ case CBM_TYPE_STRUCT:
+ // Structural equality on these is expensive and rarely needed by callers
+ // beyond pointer identity (already checked above). Treat as not-equal.
+ return false;
+ }
+ return false;
+}
+
+bool cbm_type_protocol_satisfied_by(const CBMType* proto, const CBMType* candidate) {
+ if (!proto || proto->kind != CBM_TYPE_PROTOCOL) return false;
+ if (!candidate) return false;
+ // candidate must be a NAMED or PROTOCOL type with a method-name set we
+ // can inspect. For PROTOCOL candidates, trivially satisfied if every
+ // proto method appears in candidate's method list.
+ if (candidate->kind == CBM_TYPE_PROTOCOL) {
+ if (!proto->data.protocol.method_names) return true;
+ for (int i = 0; proto->data.protocol.method_names[i]; i++) {
+ const char* needed = proto->data.protocol.method_names[i];
+ bool found = false;
+ if (candidate->data.protocol.method_names) {
+ for (int j = 0; candidate->data.protocol.method_names[j]; j++) {
+ if (str_eq_or_both_null(needed, candidate->data.protocol.method_names[j])) {
+ found = true;
+ break;
+ }
+ }
+ }
+ if (!found) return false;
+ }
+ return true;
+ }
+ // Nominal candidates require the registry — caller's responsibility.
+ return false;
+}
+
const CBMType* cbm_type_resolve_alias(const CBMType* t) {
for (int i = 0; i < 16 && t; i++) {
if (t->kind != CBM_TYPE_ALIAS) return t;
diff --git a/internal/cbm/lsp/type_rep.h b/internal/cbm/lsp/type_rep.h
index f8ab0b57a..a6ef5634a 100644
--- a/internal/cbm/lsp/type_rep.h
+++ b/internal/cbm/lsp/type_rep.h
@@ -23,6 +23,11 @@ typedef enum {
CBM_TYPE_RVALUE_REF, // T&& (C++ rvalue reference)
CBM_TYPE_TEMPLATE, // Parameterized type: vector<T> — stores template name + args
CBM_TYPE_ALIAS, // Type alias: using/typedef — stores alias name + underlying type
+ CBM_TYPE_UNION, // Python: A | B, Optional[T], Union[A, B] — sorted-canonical list
+ CBM_TYPE_LITERAL, // Python: Literal["foo", 3] — wraps a base type + literal value text
+ CBM_TYPE_PROTOCOL, // Python: typing.Protocol — like INTERFACE but matched structurally
+ CBM_TYPE_MODULE, // Python: import os; os is a module-typed binding
+ CBM_TYPE_CALLABLE, // Python: Callable[[A, B], R] — untyped-named callable variant of FUNC
} CBMTypeKind;
// Forward declaration
@@ -72,6 +77,27 @@ struct CBMType {
const char* alias_qn; // "proj.ns.MyAlias"
const CBMType* underlying; // the actual type it aliases
} alias; // ALIAS
+ struct {
+ const CBMType** members; // NULL-terminated, deduplicated, sorted by kind/qn
+ int count;
+ } union_type; // UNION
+ struct {
+ const CBMType* base; // base type (e.g. BUILTIN("int"), BUILTIN("str"))
+ const char* literal_text; // canonical text: "3", "\"foo\"", "True"
+ } literal; // LITERAL
+ struct {
+ const char* qualified_name; // e.g. "typing.Iterable"
+ const char** method_names; // NULL-terminated method names — structural matching
+ const CBMType** method_sigs; // NULL-terminated signatures (each is FUNC/CALLABLE)
+ } protocol; // PROTOCOL
+ struct {
+ const char* module_qn; // module qualified name (matches CBMImport.module_path)
+ } module; // MODULE
+ struct {
+ const CBMType** param_types; // NULL-terminated; NULL element means "Any" / unknown
+ const CBMType* return_type; // single return; for tuples wrap in CBM_TYPE_TUPLE
+ int param_count; // -1 = elliptic / Callable[..., R]
+ } callable; // CALLABLE
} data;
};
@@ -91,6 +117,18 @@ const CBMType* cbm_type_rvalue_ref(CBMArena* a, const CBMType* elem);
const CBMType* cbm_type_template(CBMArena* a, const char* name, const CBMType** args, int arg_count);
const CBMType* cbm_type_alias(CBMArena* a, const char* alias_qn, const CBMType* underlying);
+// Python-flavored constructors. UNION normalizes input: nested unions are
+// flattened, duplicates removed, single-member unions collapse to that
+// member, and the empty union is UNKNOWN. Members must be arena-allocated.
+const CBMType* cbm_type_union(CBMArena* a, const CBMType** members, int count);
+const CBMType* cbm_type_optional(CBMArena* a, const CBMType* t); // Optional[T] == Union[T, None]
+const CBMType* cbm_type_literal(CBMArena* a, const CBMType* base, const char* literal_text);
+const CBMType* cbm_type_protocol(CBMArena* a, const char* qualified_name,
+ const char** method_names, const CBMType** method_sigs);
+const CBMType* cbm_type_module(CBMArena* a, const char* module_qn);
+const CBMType* cbm_type_callable(CBMArena* a, const CBMType** param_types, int param_count,
+ const CBMType* return_type);
+
// Operations
const CBMType* cbm_type_deref(const CBMType* t); // remove one pointer level
const CBMType* cbm_type_elem(const CBMType* t); // get element type (slice/chan/pointer)
@@ -98,6 +136,20 @@ bool cbm_type_is_unknown(const CBMType* t);
bool cbm_type_is_interface(const CBMType* t);
bool cbm_type_is_pointer(const CBMType* t);
bool cbm_type_is_reference(const CBMType* t);
+bool cbm_type_is_union(const CBMType* t);
+bool cbm_type_is_protocol(const CBMType* t);
+bool cbm_type_is_module(const CBMType* t);
+
+// Structural equality on type representation (used by union dedup and
+// protocol-method-set matching). Two types are equal if their kinds match
+// and their structural members match recursively.
+bool cbm_type_equal(const CBMType* a, const CBMType* b);
+
+// Test whether `candidate` satisfies the structural protocol `proto`.
+// Walks proto.method_names against candidate's method set (NAMED → registry
+// lookup is the caller's job; this helper only matches existing method
+// signatures stored on a PROTOCOL).
+bool cbm_type_protocol_satisfied_by(const CBMType* proto, const CBMType* candidate);
// Follow alias chain with cycle detection (max 16 levels).
const CBMType* cbm_type_resolve_alias(const CBMType* t);
diff --git a/tests/test_main.c b/tests/test_main.c
index eb2ceff75..548648ec6 100644
--- a/tests/test_main.c
+++ b/tests/test_main.c
@@ -42,6 +42,7 @@ extern void suite_sqlite_writer(void);
extern void suite_go_lsp(void);
extern void suite_c_lsp(void);
extern void suite_scope(void);
+extern void suite_type_rep(void);
extern void suite_store_arch(void);
extern void suite_store_bulk(void);
extern void suite_traces(void);
@@ -114,6 +115,7 @@ int main(void) {
/* LSP resolvers */
RUN_SUITE(scope);
+ RUN_SUITE(type_rep);
RUN_SUITE(go_lsp);
RUN_SUITE(c_lsp);
diff --git a/tests/test_type_rep.c b/tests/test_type_rep.c
new file mode 100644
index 000000000..4bfc49f71
--- /dev/null
+++ b/tests/test_type_rep.c
@@ -0,0 +1,257 @@
+/*
+ * test_type_rep.c — Phase 1 type-rep extensions for Python LSP.
+ *
+ * Covers the five new kinds (UNION, LITERAL, PROTOCOL, MODULE, CALLABLE),
+ * structural equality, union normalization (flatten + dedup + collapse),
+ * Optional[T] sugar, and protocol structural matching.
+ */
+#include "test_framework.h"
+#include "cbm.h"
+#include "lsp/type_rep.h"
+
+/* ── UNION: flatten, dedup, collapse ──────────────────────────── */
+
+TEST(typerep_union_two_distinct) {
+ CBMArena a;
+ cbm_arena_init(&a);
+ const CBMType *i = cbm_type_builtin(&a, "int");
+ const CBMType *s = cbm_type_builtin(&a, "str");
+ const CBMType *m[2] = {i, s};
+ const CBMType *u = cbm_type_union(&a, m, 2);
+ ASSERT(cbm_type_is_union(u));
+ ASSERT_EQ(u->data.union_type.count, 2);
+ cbm_arena_destroy(&a);
+ PASS();
+}
+
+TEST(typerep_union_dedupes_duplicates) {
+ CBMArena a;
+ cbm_arena_init(&a);
+ const CBMType *i1 = cbm_type_builtin(&a, "int");
+ const CBMType *i2 = cbm_type_builtin(&a, "int");
+ const CBMType *m[2] = {i1, i2};
+ const CBMType *u = cbm_type_union(&a, m, 2);
+ /* dedup collapses to a single int type, not a union */
+ ASSERT(!cbm_type_is_union(u));
+ ASSERT_EQ(u->kind, CBM_TYPE_BUILTIN);
+ ASSERT_STR_EQ(u->data.builtin.name, "int");
+ cbm_arena_destroy(&a);
+ PASS();
+}
+
+TEST(typerep_union_flattens_nested) {
+ CBMArena a;
+ cbm_arena_init(&a);
+ const CBMType *i = cbm_type_builtin(&a, "int");
+ const CBMType *s = cbm_type_builtin(&a, "str");
+ const CBMType *b = cbm_type_builtin(&a, "bytes");
+ const CBMType *inner_pair[2] = {i, s};
+ const CBMType *inner = cbm_type_union(&a, inner_pair, 2);
+ const CBMType *outer_pair[2] = {inner, b};
+ const CBMType *u = cbm_type_union(&a, outer_pair, 2);
+ /* flattening yields 3 distinct members */
+ ASSERT(cbm_type_is_union(u));
+ ASSERT_EQ(u->data.union_type.count, 3);
+ cbm_arena_destroy(&a);
+ PASS();
+}
+
+TEST(typerep_union_single_member_collapses) {
+ CBMArena a;
+ cbm_arena_init(&a);
+ const CBMType *i = cbm_type_builtin(&a, "int");
+ const CBMType *m[1] = {i};
+ const CBMType *u = cbm_type_union(&a, m, 1);
+ ASSERT(!cbm_type_is_union(u));
+ ASSERT_EQ(u->kind, CBM_TYPE_BUILTIN);
+ cbm_arena_destroy(&a);
+ PASS();
+}
+
+TEST(typerep_union_empty_is_unknown) {
+ CBMArena a;
+ cbm_arena_init(&a);
+ const CBMType *u = cbm_type_union(&a, NULL, 0);
+ ASSERT(cbm_type_is_unknown(u));
+ cbm_arena_destroy(&a);
+ PASS();
+}
+
+TEST(typerep_optional_is_union_with_none) {
+ CBMArena a;
+ cbm_arena_init(&a);
+ const CBMType *i = cbm_type_builtin(&a, "int");
+ const CBMType *opt = cbm_type_optional(&a, i);
+ ASSERT(cbm_type_is_union(opt));
+ ASSERT_EQ(opt->data.union_type.count, 2);
+ /* membership: int + None */
+ bool has_int = false, has_none = false;
+ for (int k = 0; k < opt->data.union_type.count; k++) {
+ const CBMType *m = opt->data.union_type.members[k];
+ if (m->kind == CBM_TYPE_BUILTIN && strcmp(m->data.builtin.name, "int") == 0) has_int = true;
+ if (m->kind == CBM_TYPE_BUILTIN && strcmp(m->data.builtin.name, "None") == 0) has_none = true;
+ }
+ ASSERT(has_int);
+ ASSERT(has_none);
+ cbm_arena_destroy(&a);
+ PASS();
+}
+
+/* ── LITERAL ───────────────────────────────────────────────────── */
+
+TEST(typerep_literal_int_3) {
+ CBMArena a;
+ cbm_arena_init(&a);
+ const CBMType *base = cbm_type_builtin(&a, "int");
+ const CBMType *lit = cbm_type_literal(&a, base, "3");
+ ASSERT_EQ(lit->kind, CBM_TYPE_LITERAL);
+ ASSERT(cbm_type_equal(lit->data.literal.base, base));
+ ASSERT_STR_EQ(lit->data.literal.literal_text, "3");
+ cbm_arena_destroy(&a);
+ PASS();
+}
+
+TEST(typerep_literal_equality_distinguishes_text) {
+ CBMArena a;
+ cbm_arena_init(&a);
+ const CBMType *base = cbm_type_builtin(&a, "str");
+ const CBMType *foo = cbm_type_literal(&a, base, "\"foo\"");
+ const CBMType *bar = cbm_type_literal(&a, base, "\"bar\"");
+ const CBMType *foo2 = cbm_type_literal(&a, base, "\"foo\"");
+ ASSERT(!cbm_type_equal(foo, bar));
+ ASSERT(cbm_type_equal(foo, foo2));
+ cbm_arena_destroy(&a);
+ PASS();
+}
+
+/* ── PROTOCOL ──────────────────────────────────────────────────── */
+
+TEST(typerep_protocol_method_set) {
+ CBMArena a;
+ cbm_arena_init(&a);
+ const char *methods[] = {"read", "close", NULL};
+ const CBMType *proto = cbm_type_protocol(&a, "typing.IO", methods, NULL);
+ ASSERT(cbm_type_is_protocol(proto));
+ ASSERT_STR_EQ(proto->data.protocol.qualified_name, "typing.IO");
+ ASSERT_NOT_NULL(proto->data.protocol.method_names);
+ ASSERT_STR_EQ(proto->data.protocol.method_names[0], "read");
+ ASSERT_STR_EQ(proto->data.protocol.method_names[1], "close");
+ cbm_arena_destroy(&a);
+ PASS();
+}
+
+TEST(typerep_protocol_satisfied_by_protocol_with_superset) {
+ CBMArena a;
+ cbm_arena_init(&a);
+ const char *needed[] = {"read", "close", NULL};
+ const char *have[] = {"read", "write", "close", "flush", NULL};
+ const CBMType *proto = cbm_type_protocol(&a, "P1", needed, NULL);
+ const CBMType *cand = cbm_type_protocol(&a, "P2", have, NULL);
+ ASSERT(cbm_type_protocol_satisfied_by(proto, cand));
+ cbm_arena_destroy(&a);
+ PASS();
+}
+
+TEST(typerep_protocol_unsatisfied_when_method_missing) {
+ CBMArena a;
+ cbm_arena_init(&a);
+ const char *needed[] = {"read", "close", NULL};
+ const char *have[] = {"read", NULL};
+ const CBMType *proto = cbm_type_protocol(&a, "P1", needed, NULL);
+ const CBMType *cand = cbm_type_protocol(&a, "P2", have, NULL);
+ ASSERT(!cbm_type_protocol_satisfied_by(proto, cand));
+ cbm_arena_destroy(&a);
+ PASS();
+}
+
+/* ── MODULE ────────────────────────────────────────────────────── */
+
+TEST(typerep_module_carries_qn) {
+ CBMArena a;
+ cbm_arena_init(&a);
+ const CBMType *m = cbm_type_module(&a, "os.path");
+ ASSERT(cbm_type_is_module(m));
+ ASSERT_STR_EQ(m->data.module.module_qn, "os.path");
+ cbm_arena_destroy(&a);
+ PASS();
+}
+
+TEST(typerep_module_equality_by_qn) {
+ CBMArena a;
+ cbm_arena_init(&a);
+ const CBMType *a1 = cbm_type_module(&a, "os");
+ const CBMType *a2 = cbm_type_module(&a, "os");
+ const CBMType *b = cbm_type_module(&a, "sys");
+ ASSERT(cbm_type_equal(a1, a2));
+ ASSERT(!cbm_type_equal(a1, b));
+ cbm_arena_destroy(&a);
+ PASS();
+}
+
+/* ── CALLABLE ──────────────────────────────────────────────────── */
+
+TEST(typerep_callable_with_args_and_return) {
+ CBMArena a;
+ cbm_arena_init(&a);
+ const CBMType *i = cbm_type_builtin(&a, "int");
+ const CBMType *s = cbm_type_builtin(&a, "str");
+ const CBMType *params[2] = {i, s};
+ const CBMType *c = cbm_type_callable(&a, params, 2, i);
+ ASSERT_EQ(c->kind, CBM_TYPE_CALLABLE);
+ ASSERT_EQ(c->data.callable.param_count, 2);
+ ASSERT(cbm_type_equal(c->data.callable.return_type, i));
+ cbm_arena_destroy(&a);
+ PASS();
+}
+
+TEST(typerep_callable_elliptic_arity_minus_one) {
+ /* Callable[..., R] — variadic in Python type-hint sense. */
+ CBMArena a;
+ cbm_arena_init(&a);
+ const CBMType *r = cbm_type_builtin(&a, "int");
+ const CBMType *c = cbm_type_callable(&a, NULL, -1, r);
+ ASSERT_EQ(c->data.callable.param_count, -1);
+ cbm_arena_destroy(&a);
+ PASS();
+}
+
+TEST(typerep_callable_equality) {
+ CBMArena a;
+ cbm_arena_init(&a);
+ const CBMType *i = cbm_type_builtin(&a, "int");
+ const CBMType *s = cbm_type_builtin(&a, "str");
+ const CBMType *p1[1] = {i};
+ const CBMType *c1 = cbm_type_callable(&a, p1, 1, s);
+ const CBMType *c2 = cbm_type_callable(&a, p1, 1, s);
+ const CBMType *c3 = cbm_type_callable(&a, p1, 1, i); /* different return */
+ ASSERT(cbm_type_equal(c1, c2));
+ ASSERT(!cbm_type_equal(c1, c3));
+ cbm_arena_destroy(&a);
+ PASS();
+}
+
+/* ── Suite ─────────────────────────────────────────────────────── */
+
+SUITE(type_rep) {
+ /* UNION */
+ RUN_TEST(typerep_union_two_distinct);
+ RUN_TEST(typerep_union_dedupes_duplicates);
+ RUN_TEST(typerep_union_flattens_nested);
+ RUN_TEST(typerep_union_single_member_collapses);
+ RUN_TEST(typerep_union_empty_is_unknown);
+ RUN_TEST(typerep_optional_is_union_with_none);
+ /* LITERAL */
+ RUN_TEST(typerep_literal_int_3);
+ RUN_TEST(typerep_literal_equality_distinguishes_text);
+ /* PROTOCOL */
+ RUN_TEST(typerep_protocol_method_set);
+ RUN_TEST(typerep_protocol_satisfied_by_protocol_with_superset);
+ RUN_TEST(typerep_protocol_unsatisfied_when_method_missing);
+ /* MODULE */
+ RUN_TEST(typerep_module_carries_qn);
+ RUN_TEST(typerep_module_equality_by_qn);
+ /* CALLABLE */
+ RUN_TEST(typerep_callable_with_args_and_return);
+ RUN_TEST(typerep_callable_elliptic_arity_minus_one);
+ RUN_TEST(typerep_callable_equality);
+}