Skip to main content

t_ree/
declaration.rs

1use crate::expression::{Block, Expression, Span};
2use crate::types::Type;
3
4/// A named function parameter.
5#[derive(Clone, Debug)]
6pub struct Parameter {
7    /// Parameter name.
8    pub name: String,
9    /// Parameter type (`None` for label parameters with inferred types).
10    pub parameter_type: Option<Type>,
11}
12
13/// Parameter list, either fixed or variadic.
14#[derive(Clone, Debug)]
15pub enum ParameterList {
16    /// Fixed number of parameters.
17    Fixed(Vec<Parameter>),
18    /// Variadic (last parameter accepts additional arguments).
19    Variadic(Vec<Parameter>),
20}
21
22impl ParameterList {
23    /// Returns the parameter slice.
24    pub fn parameters(&self) -> &[Parameter] {
25        match self {
26            Self::Fixed(parameters) | Self::Variadic(parameters) => parameters,
27        }
28    }
29}
30
31/// Function definition with body.
32#[derive(Clone, Debug)]
33pub struct FunctionDefinition {
34    /// Function name.
35    pub name: String,
36    /// Function parameters.
37    pub parameters: Vec<Parameter>,
38    /// Return type.
39    pub return_type: Type,
40    /// Function body.
41    pub body: Block,
42    /// Whether the function is publicly visible (not `static` in C).
43    pub public: bool,
44    /// Source location.
45    pub span: Span,
46}
47
48/// External function declaration (no body).
49#[derive(Clone, Debug)]
50pub struct ExternFunction {
51    /// Function name.
52    pub name: String,
53    /// Parameter list (may be variadic).
54    pub parameters: ParameterList,
55    /// Return type.
56    pub return_type: Type,
57}
58
59/// Newtype declaration: creates a distinct type wrapping another.
60///
61/// Implicit downcast (newtype → underlying) is allowed,
62/// but upcast (underlying → newtype) requires explicit construction.
63#[derive(Clone, Debug)]
64pub struct Newtype {
65    /// Newtype name.
66    pub name: String,
67    /// The underlying type being wrapped.
68    pub inner_type: Type,
69    /// Whether the type is publicly visible (for future header generation).
70    pub public: bool,
71}
72
73/// Compile-time constant declaration.
74#[derive(Clone, Debug)]
75pub struct Constant {
76    /// Constant name.
77    pub name: String,
78    /// Constant type.
79    pub constant_type: Type,
80    /// Constant value expression.
81    pub value: Expression,
82    /// Whether the constant is publicly visible (not `static` in C).
83    pub public: bool,
84}
85
86/// Top-level declaration.
87#[derive(Clone, Debug)]
88pub enum Declaration {
89    /// Newtype definition.
90    Type(Newtype),
91    /// Function definition.
92    Function(FunctionDefinition),
93    /// External function declaration.
94    Extern(ExternFunction),
95    /// Constant definition.
96    Constant(Constant),
97    /// Module import.
98    Import(String),
99}
100
101/// A module is a list of declarations.
102pub type Module = Vec<Declaration>;