Ty → T Interop Guide

How Ty compiles down to T

Ty compiles to T, which then compiles to C. This guide explains how Ty constructs map to T constructs, and ultimately to C code.

Compilation Pipeline

Ty source (.c.ty / .sln.ty)
  → Import expansion (ty-expand)
  → Ty AST (ty-parser / ty-parser_sln)
  → Unused-binding lint and borrow/move check (ty-ree)
  → Monomorphization (ty-backend_t)
  → Automatic drop insertion (ty-backend_t)
  → Lowering (ty-backend_t)
  → T AST (t-ree)
  → Type Resolution (t-ree/resolve)
  → C code (t-backend_c)
  → Binary (cc)

A program that fails the lint or the borrow check stops before monomorphization, so no C is generated for it.

Monomorphization

Generic types and functions are duplicated for each concrete instantiation. No generics survive into the T output.

Type Mangling

TyT (after monomorphization)
identity[T] with T = i32impl_identity$i32 (in C)
identity[T] with T = boolimpl_identity$bool (in C)
Point[T] with T = f64Point$f64
x[T] with T = f64x$f64

The $ separator prevents collisions with user-defined names (e.g. a manual identity_i32 won't clash). A generic fn instance keeps the mangled type name, and its body is emitted as an impl_ function (see Impl Blocks).

Monomorphization is transitive: Point[f64] triggers x[f64] and y[f64]. Field access is rewritten: p.x on a Point$f64 becomes p.x$f64.

Const Generic Mangling

TyT (after monomorphization)
Buffer[i32]4Buffer$i32$4
[T](N * 2) with N = 3[T]6
MV[f64]0b1110MV$f64$14

Const expressions are evaluated during monomorphization. Arithmetic, bitwise operations, and const function calls are resolved to concrete values. Named constants (const VEC3: u64 = 0b1110) are substituted before monomorphization.

Lowering

After monomorphization, Ty-specific constructs are lowered to T equivalents.

Impl Blocks

Each impl block becomes a function named impl_ plus the type name. The type itself stays, so a value of it can still be built as data:

TyC
type n = i32;
type twice = n;
impl twice -> i32 { n.i32 * 2 }
typedef int32_t n;
typedef n twice;
static int32_t impl_twice(n _n) {
    return ((int32_t)_n * 2);
}

The impl body receives the tuple fields as parameters, each prefixed with _. it references become the first parameter.

Callable Construction

Construction of a type with an impl block becomes a call of its impl_ function:

TyC
twice(5)impl_twice(5)
add(3, 7)impl_add(3, 7)
identity(true)impl_identity$bool(true)

Type Mapping

All Ty types map directly to T types:

TyTC
type x = f32;type x = f32;typedef float x;
type Point = x & y;type Point = x & y;struct Point { x x; y y; };
type MV = [f64]3;type MV = [f64]3;struct MV { double data[3]; };
fn twice n -> i32 { … }a type twice with an impltypedef n twice; plus static int32_t impl_twice(n _n)

Array newtypes are emitted as struct wrappers in C (not typedef), because C cannot return or cast bare arrays.

Struct Layout

Ty tuple newtypes have the same memory layout as C structs. Fields are ordered as declared, with platform-default alignment. This means Ty structs are ABI-compatible with equivalent C structs.

C Output

Use the code subcommand to inspect the generated code at any stage:

ty code example.c.ty          # show generated C
ty code --rust example.c.ty   # show generated safe Rust
ty build example.c.ty -o out  # compile to binary 'out'

The generated C uses -std=gnu11 with GCC statement expressions for block expressions. See the T → C Interop Guide for the full C mapping.

Ty also compiles directly to safe Rust, bypassing T and keeping generics generic — see the Ty → Rust Interop Guide.