ty_ree/declaration.rs
1//! Declarations: types, impl blocks, imports, constants, and extern functions.
2
3use crate::expression::{Block, Expression};
4use crate::types::{GenericParameter, Type};
5
6/// A named, typed function parameter.
7#[derive(Clone, Debug)]
8pub struct Parameter {
9 /// Parameter name.
10 pub name: String,
11 /// Parameter type.
12 pub parameter_type: Type,
13}
14
15/// Fixed-arity or variadic parameter list.
16#[derive(Clone, Debug)]
17pub enum ParameterList {
18 /// Fixed number of parameters.
19 Fixed(Vec<Parameter>),
20 /// Variadic (last parameter repeats).
21 Variadic(Vec<Parameter>),
22}
23
24impl ParameterList {
25 /// Returns the parameter slice regardless of arity kind.
26 pub fn parameters(&self) -> &[Parameter] {
27 match self {
28 Self::Fixed(parameters) | Self::Variadic(parameters) => parameters,
29 }
30 }
31}
32
33/// Makes a type callable: `impl Type -> R { body }`.
34///
35/// Constructing the type auto-evaluates the body.
36/// Each type has at most one impl block.
37#[derive(Clone, Debug)]
38pub struct ImplBlock {
39 /// Generic type/const parameters.
40 pub generic_parameters: Vec<GenericParameter>,
41 /// The type being made callable.
42 pub target_type: Type,
43 /// Return type and body.
44 pub callable: Callable,
45 /// Whether this impl is evaluated at compile time (`const fn`).
46 pub const_fn: bool,
47}
48
49/// Return type and body of a callable impl.
50#[derive(Clone, Debug)]
51pub struct Callable {
52 /// Return type of the callable.
53 pub return_type: Type,
54 /// Body executed on call.
55 pub body: Block,
56 /// Original parameter names when desugared from `fn`.
57 /// If present, the body uses these directly instead of `it.field`.
58 pub parameters: Option<Vec<Parameter>>,
59}
60
61/// Foreign function declaration (`extern fn`).
62#[derive(Clone, Debug)]
63pub struct ExternFunction {
64 /// Symbol name.
65 pub name: String,
66 /// Parameter list (may be variadic).
67 pub parameters: ParameterList,
68 /// Return type.
69 pub return_type: Type,
70}
71
72/// A newtype declaration (`type Name = Inner`).
73#[derive(Clone, Debug)]
74pub struct Newtype {
75 /// Type name.
76 pub name: String,
77 /// Generic type/const parameters.
78 pub generic_parameters: Vec<GenericParameter>,
79 /// Underlying wrapped type.
80 pub inner_type: Type,
81 /// Whether the type has external linkage.
82 pub public: bool,
83}
84
85/// A compile-time constant declaration.
86#[derive(Clone, Debug)]
87pub struct Constant {
88 /// Constant name.
89 pub name: String,
90 /// Type of the constant.
91 pub constant_type: Type,
92 /// Constant value expression.
93 pub value: Expression,
94 /// Whether the constant has external linkage.
95 pub public: bool,
96}
97
98/// A top-level declaration.
99#[derive(Clone, Debug)]
100pub enum Declaration {
101 /// Newtype declaration.
102 Type(Newtype),
103 /// Impl block making a type callable.
104 Impl(ImplBlock),
105 /// Foreign function import.
106 Extern(ExternFunction),
107 /// Compile-time constant.
108 Constant(Constant),
109 /// Module import by path.
110 Import(String),
111}
112
113/// A module is a sequence of declarations.
114pub type Module = Vec<Declaration>;