ty-ree 0.1.0

AST definitions for the Ty programming language
Documentation
//! Type system: primitives, generics, lifetimes, and const expressions.

pub use t_ree::types::{FloatWidth, IntWidth, Mutability, Signedness};

/// A generic type or const parameter declaration.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GenericParameter {
    /// Parameter name.
    pub name: String,
    /// Whether this is a type or const parameter.
    pub kind: GenericParameterKind,
}

/// Distinguishes type parameters from const parameters.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum GenericParameterKind {
    /// A type parameter (`T`).
    Type,
    /// A const parameter with its type (`N: usize`).
    Const(Type),
}

/// A lifetime annotation.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Lifetime {
    /// Explicitly named lifetime (`'a`).
    Named(String),
    /// Lifetime to be inferred by the compiler.
    Inferred,
}

/// Signature of a function pointer type.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FunctionSignature {
    /// Parameter types.
    pub parameters: Vec<Type>,
    /// Return type.
    pub return_type: Box<Type>,
}

/// A Ty type.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Type {
    /// The never/bottom type (diverges).
    Never,
    /// Boolean type.
    Bool,
    /// Integer type with width and signedness.
    Int(IntWidth, Signedness),
    /// Floating-point type with width.
    Float(FloatWidth),
    /// Raw pointer with mutability, lifetime, and pointee type.
    Pointer(Mutability, Lifetime, Box<Self>),
    /// Reference with mutability, lifetime, and referent type.
    Reference(Mutability, Lifetime, Box<Self>),
    /// Fixed-size array (`[T; N]`).
    Array(Box<Self>, ConstExpression),
    /// SIMD vector type.
    Vector(Box<Self>, ConstExpression),
    /// Slice type with mutability.
    Slice(Mutability, Box<Self>),
    /// Product type (tuple/struct).
    Tuple(Vec<Self>),
    /// Sum type (enum/variant).
    Enum(Vec<Self>),
    /// Named user-defined type with generic arguments.
    Named(String, Vec<GenericArgument>),
    /// Function pointer type.
    Function(FunctionSignature),
    /// Unresolved generic type parameter reference.
    Generic(String),
    /// Type to be inferred.
    Inferred,
}

/// A concrete argument supplied for a generic parameter.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum GenericArgument {
    /// A type argument.
    Type(Type),
    /// A lifetime argument.
    Lifetime(Lifetime),
    /// A const expression argument.
    Const(ConstExpression),
}

/// A compile-time constant expression used in types (array lengths, const generics).
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ConstExpression {
    /// Integer literal.
    Integer(u128),
    /// Named constant or const parameter.
    Named(String),
    /// Addition.
    Add(Box<Self>, Box<Self>),
    /// Subtraction.
    Subtract(Box<Self>, Box<Self>),
    /// Multiplication.
    Multiply(Box<Self>, Box<Self>),
    /// Division.
    Divide(Box<Self>, Box<Self>),
    /// Remainder.
    Modulo(Box<Self>, Box<Self>),
    /// Bitwise AND.
    BitwiseAnd(Box<Self>, Box<Self>),
    /// Bitwise OR.
    BitwiseOr(Box<Self>, Box<Self>),
    /// Bitwise XOR.
    BitwiseXor(Box<Self>, Box<Self>),
    /// Left shift.
    ShiftLeft(Box<Self>, Box<Self>),
    /// Right shift.
    ShiftRight(Box<Self>, Box<Self>),
    /// Const function call.
    Call(String, Vec<Self>),
    /// Field access on a const expression.
    Field(Box<Self>, String),
    /// Named tuple construction (`{ rows: 3, columns: 3 }`).
    Construction(Vec<(String, Self)>),
}

impl Type {
    /// Returns the unit type (empty tuple).
    pub fn unit() -> Self {
        Self::Tuple(Vec::new())
    }

    /// Returns true if this is the unit type.
    pub fn is_unit(&self) -> bool {
        matches!(self, Self::Tuple(fields) if fields.is_empty())
    }
}