ty_ree/types.rs
1//! Type system: primitives, generics, lifetimes, and const expressions.
2
3pub use t_ree::types::{FloatWidth, IntWidth, Mutability, Signedness};
4
5/// A generic type or const parameter declaration.
6#[derive(Clone, Debug, PartialEq, Eq)]
7pub struct GenericParameter {
8 /// Parameter name.
9 pub name: String,
10 /// Whether this is a type or const parameter.
11 pub kind: GenericParameterKind,
12}
13
14/// Distinguishes type parameters from const parameters.
15#[derive(Clone, Debug, PartialEq, Eq)]
16pub enum GenericParameterKind {
17 /// A type parameter (`T`).
18 Type,
19 /// A const parameter with its type (`N: usize`).
20 Const(Type),
21}
22
23/// A lifetime annotation.
24#[derive(Clone, Debug, PartialEq, Eq)]
25pub enum Lifetime {
26 /// Explicitly named lifetime (`'a`).
27 Named(String),
28 /// Lifetime to be inferred by the compiler.
29 Inferred,
30}
31
32/// Signature of a function pointer type.
33#[derive(Clone, Debug, PartialEq, Eq)]
34pub struct FunctionSignature {
35 /// Parameter types.
36 pub parameters: Vec<Type>,
37 /// Return type.
38 pub return_type: Box<Type>,
39}
40
41/// A Ty type.
42#[derive(Clone, Debug, PartialEq, Eq)]
43pub enum Type {
44 /// The never/bottom type (diverges).
45 Never,
46 /// Boolean type.
47 Bool,
48 /// Integer type with width and signedness.
49 Int(IntWidth, Signedness),
50 /// Floating-point type with width.
51 Float(FloatWidth),
52 /// Raw pointer with mutability, lifetime, and pointee type.
53 Pointer(Mutability, Lifetime, Box<Self>),
54 /// Reference with mutability, lifetime, and referent type.
55 Reference(Mutability, Lifetime, Box<Self>),
56 /// Fixed-size array (`[T; N]`).
57 Array(Box<Self>, ConstExpression),
58 /// SIMD vector type.
59 Vector(Box<Self>, ConstExpression),
60 /// Slice type with mutability.
61 Slice(Mutability, Box<Self>),
62 /// Product type (tuple/struct).
63 Tuple(Vec<Self>),
64 /// Sum type (enum/variant).
65 Enum(Vec<Self>),
66 /// Named user-defined type with generic arguments.
67 Named(String, Vec<GenericArgument>),
68 /// Function pointer type.
69 Function(FunctionSignature),
70 /// Unresolved generic type parameter reference.
71 Generic(String),
72 /// Type to be inferred.
73 Inferred,
74}
75
76/// A concrete argument supplied for a generic parameter.
77#[derive(Clone, Debug, PartialEq, Eq)]
78pub enum GenericArgument {
79 /// A type argument.
80 Type(Type),
81 /// A lifetime argument.
82 Lifetime(Lifetime),
83 /// A const expression argument.
84 Const(ConstExpression),
85}
86
87/// A compile-time constant expression used in types (array lengths, const generics).
88#[derive(Clone, Debug, PartialEq, Eq)]
89pub enum ConstExpression {
90 /// Integer literal.
91 Integer(u128),
92 /// Named constant or const parameter.
93 Named(String),
94 /// Addition.
95 Add(Box<Self>, Box<Self>),
96 /// Subtraction.
97 Subtract(Box<Self>, Box<Self>),
98 /// Multiplication.
99 Multiply(Box<Self>, Box<Self>),
100 /// Division.
101 Divide(Box<Self>, Box<Self>),
102 /// Remainder.
103 Modulo(Box<Self>, Box<Self>),
104 /// Bitwise AND.
105 BitwiseAnd(Box<Self>, Box<Self>),
106 /// Bitwise OR.
107 BitwiseOr(Box<Self>, Box<Self>),
108 /// Bitwise XOR.
109 BitwiseXor(Box<Self>, Box<Self>),
110 /// Left shift.
111 ShiftLeft(Box<Self>, Box<Self>),
112 /// Right shift.
113 ShiftRight(Box<Self>, Box<Self>),
114 /// Const function call.
115 Call(String, Vec<Self>),
116 /// Field access on a const expression.
117 Field(Box<Self>, String),
118 /// Named tuple construction (`{ rows: 3, columns: 3 }`).
119 Construction(Vec<(String, Self)>),
120}
121
122impl Type {
123 /// Returns the unit type (empty tuple).
124 pub fn unit() -> Self {
125 Self::Tuple(Vec::new())
126 }
127
128 /// Returns true if this is the unit type.
129 pub fn is_unit(&self) -> bool {
130 matches!(self, Self::Tuple(fields) if fields.is_empty())
131 }
132}