1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use super::reg::GetUsedRegs;
use super::val::MutableValue;
use super::{ty::DataType, ResourceLocation};
use super::{Identifier, Value};
use std::fmt::Debug;
use std::hash::Hash;

#[derive(Clone)]
pub struct FunctionInterface {
	pub id: ResourceLocation,
	pub sig: FunctionSignature,
	pub annotations: FunctionAnnotations,
}

impl FunctionInterface {
	pub fn new(id: ResourceLocation) -> Self {
		Self::with_signature(id, FunctionSignature::new())
	}

	pub fn with_signature(id: ResourceLocation, sig: FunctionSignature) -> Self {
		Self::with_all(id, sig, FunctionAnnotations::new())
	}

	pub fn with_all(
		id: ResourceLocation,
		sig: FunctionSignature,
		annotations: FunctionAnnotations,
	) -> Self {
		Self {
			id,
			sig,
			annotations,
		}
	}
}

impl Debug for FunctionInterface {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		write!(f, "{}{:?}", self.id, self.sig)
	}
}

impl Hash for FunctionInterface {
	fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
		self.id.hash(state)
	}
}

impl PartialEq for FunctionInterface {
	fn eq(&self, other: &Self) -> bool {
		self.id.eq(&other.id)
	}
}

impl Eq for FunctionInterface {}

impl Default for FunctionInterface {
	fn default() -> Self {
		Self::new("".into())
	}
}

pub type FunctionParams = Vec<DataType>;
pub type FunctionArgs = Vec<Value>;

#[derive(Clone, PartialEq, Eq)]
pub struct FunctionSignature {
	pub params: FunctionParams,
	pub ret: ReturnType,
}

impl FunctionSignature {
	pub fn new() -> Self {
		Self::with_all(FunctionParams::new(), ReturnType::Void)
	}

	pub fn with_params(params: FunctionParams) -> Self {
		Self::with_all(params, ReturnType::Void)
	}

	pub fn with_ret(ret: ReturnType) -> Self {
		Self::with_all(FunctionParams::new(), ret)
	}

	pub fn with_all(params: FunctionParams, ret: ReturnType) -> Self {
		Self { params, ret }
	}
}

impl Default for FunctionSignature {
	fn default() -> Self {
		Self::new()
	}
}

impl Debug for FunctionSignature {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		write!(f, "(")?;
		for (i, param) in self.params.iter().enumerate() {
			param.fmt(f)?;
			if i != self.params.len() - 1 {
				write!(f, ",")?;
			}
		}
		write!(f, "): {:?}", self.ret)?;

		Ok(())
	}
}

#[derive(Clone, PartialEq, Eq)]
pub enum ReturnType {
	Void,
	Standard(Vec<DataType>),
}

impl Debug for ReturnType {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			Self::Void => write!(f, "void"),
			Self::Standard(ty) => ty.fmt(f),
		}
	}
}

#[derive(Clone, PartialEq)]
pub struct CallInterface {
	pub function: ResourceLocation,
	pub args: FunctionArgs,
	pub ret: Vec<MutableValue>,
}

impl CallInterface {
	pub fn iter_used_regs_mut(&mut self) -> impl Iterator<Item = &mut Identifier> {
		let args = self
			.args
			.iter_mut()
			.flat_map(|x| x.get_used_regs_mut().into_iter());
		let ret = self
			.ret
			.iter_mut()
			.flat_map(|x| x.get_used_regs_mut().into_iter());
		args.chain(ret)
	}
}

impl GetUsedRegs for CallInterface {
	fn append_used_regs<'a>(&'a self, regs: &mut Vec<&'a Identifier>) {
		for arg in &self.args {
			arg.append_used_regs(regs);
		}
		for ret in &self.ret {
			ret.append_used_regs(regs);
		}
	}
}

impl Debug for CallInterface {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		write!(f, "{}(", self.function)?;
		for (i, arg) in self.args.iter().enumerate() {
			arg.fmt(f)?;
			if i != self.args.len() - 1 {
				write!(f, ",")?;
			}
		}
		write!(f, ")")?;

		Ok(())
	}
}

#[derive(Debug, Clone)]
pub struct FunctionAnnotations {
	pub preserve: bool,
	pub no_inline: bool,
	pub no_strip: bool,
}

impl FunctionAnnotations {
	pub fn new() -> Self {
		Self {
			preserve: false,
			no_inline: false,
			no_strip: false,
		}
	}
}

impl Default for FunctionAnnotations {
	fn default() -> Self {
		Self::new()
	}
}