yog_script_analyze/typeck/
types.rs1use std::collections::HashMap;
6
7use super::model::Type;
8use crate::ast::Expr;
9
10#[derive(Debug, Default)]
14pub struct Types {
15 types: HashMap<usize, Type>,
16 operators: HashMap<usize, OperatorUse>,
19 conversions: HashMap<usize, OperatorUse>,
22 recasts: HashMap<usize, Type>,
25 quoted: HashMap<usize, Quoted>,
28 items: HashMap<usize, Type>,
30 bindings: HashMap<usize, Type>,
32 constructors: HashMap<usize, usize>,
35 constructor_rests: HashMap<usize, (usize, Type)>,
38 methods: HashMap<usize, MethodUse>,
40 messages: HashMap<usize, Message>,
42 assigned: HashMap<usize, OperatorUse>,
45 locals: std::collections::HashSet<usize>,
48 bounded: HashMap<usize, Type>,
51 moved_away: HashMap<usize, Vec<String>>,
54 templates: HashMap<usize, super::templates::MessageTemplate>,
56 captures: HashMap<usize, Vec<super::threads::Capture>>,
58}
59
60#[derive(Debug, Clone, PartialEq)]
63pub struct Quoted {
64 pub delegate: Type,
65 pub params: Vec<Type>,
66 pub ret: Type,
67}
68
69#[derive(Debug, Clone, PartialEq)]
72pub struct OperatorUse {
73 pub owner: Type,
74 pub method: String,
75}
76
77#[derive(Debug, Clone, PartialEq)]
83pub struct Message {
84 pub method: String,
85 pub to_server: bool,
87}
88
89#[derive(Debug, Clone, PartialEq)]
90pub struct MethodUse {
91 pub owner: Option<String>,
92 pub owner_arity: usize,
94 pub name: String,
95 pub overload: usize,
96 pub params: Vec<Type>,
97 pub ret: Type,
98 pub taken: Vec<Type>,
100 pub type_args: Vec<Type>,
102 pub extension: bool,
104 pub generics: usize,
106 pub rest_from: Option<usize>,
109}
110
111impl Types {
112 pub fn quoted(&self, e: &Expr) -> Option<&Quoted> {
116 self.quoted.get(&key(e))
117 }
118
119 pub(super) fn set_quoted(&mut self, e: &Expr, quoted: Quoted) {
120 self.quoted.insert(key(e), quoted);
121 }
122
123 pub fn recast(&self, e: &Expr) -> Option<&Type> {
124 self.recasts.get(&key(e))
125 }
126
127 pub(super) fn set_recast(&mut self, e: &Expr, to: Type) {
128 self.recasts.insert(key(e), to);
129 }
130
131 pub fn item(&self, iter: &Expr) -> Option<&Type> {
134 self.items.get(&key(iter))
135 }
136
137 pub(super) fn set_item(&mut self, iter: &Expr, t: Type) {
138 self.items.insert(key(iter), t);
139 }
140
141 pub fn binding(&self, n: &crate::ast::Name) -> Option<&Type> {
143 self.bindings.get(&(std::ptr::from_ref(n) as usize))
144 }
145
146 pub(super) fn set_binding(&mut self, n: &crate::ast::Name, t: Type) {
147 self.bindings.insert(std::ptr::from_ref(n) as usize, t);
148 }
149
150 pub fn constructor(&self, e: &Expr) -> Option<usize> {
152 self.constructors.get(&key(e)).copied()
153 }
154
155 pub fn constructor_rest(&self, e: &Expr) -> Option<&(usize, Type)> {
158 self.constructor_rests.get(&key(e))
159 }
160
161 pub(super) fn set_constructor_rest(&mut self, e: &Expr, from: usize, item: Type) {
162 self.constructor_rests.insert(key(e), (from, item));
163 }
164
165 pub(super) fn set_constructor(&mut self, e: &Expr, index: usize) {
166 self.constructors.insert(key(e), index);
167 }
168
169 pub fn method(&self, e: &Expr) -> Option<&MethodUse> {
170 self.methods.get(&key(e))
171 }
172
173 pub fn message(&self, e: &Expr) -> Option<&Message> {
175 self.messages.get(&key(e))
176 }
177
178 pub(super) fn set_message(&mut self, e: &Expr, m: Message) {
179 self.messages.insert(key(e), m);
180 }
181
182 pub fn is_local(&self, e: &Expr) -> bool {
184 self.locals.contains(&key(e))
185 }
186
187 pub(super) fn captures(&self, e: &Expr) -> &[super::threads::Capture] {
190 self.captures.get(&key(e)).map_or(&[], Vec::as_slice)
191 }
192
193 pub fn moved_away(&self, e: &Expr) -> &[String] {
196 self.moved_away.get(&key(e)).map_or(&[], Vec::as_slice)
197 }
198
199 pub(super) fn set_moved_away(&mut self, e: &Expr, names: Vec<String>) {
200 self.moved_away.insert(key(e), names);
201 }
202
203 pub(super) fn capture(&mut self, lambda: usize, c: super::threads::Capture) {
205 let all = self.captures.entry(lambda).or_default();
206 if !all.iter().any(|o| o.name == c.name) {
207 all.push(c);
208 }
209 }
210
211 pub fn bounded(&self, e: &Expr) -> Option<&Type> {
213 self.bounded.get(&key(e))
214 }
215
216 pub(super) fn set_bounded(&mut self, e: &Expr, t: Type) {
217 self.bounded.insert(key(e), t);
218 }
219
220 pub fn template(&self, e: &Expr) -> Option<&super::templates::MessageTemplate> {
222 self.templates.get(&key(e))
223 }
224
225 pub(super) fn set_template(&mut self, e: &Expr, t: super::templates::MessageTemplate) {
226 self.set(e, Type::String);
227 self.templates.insert(key(e), t);
228 }
229
230 pub(super) fn key_of(e: &Expr) -> usize {
231 key(e)
232 }
233
234 pub(super) fn set_local(&mut self, e: &Expr) {
235 self.locals.insert(key(e));
236 }
237
238 pub(super) fn set_method(&mut self, e: &Expr, used: MethodUse) {
239 self.methods.insert(key(e), used);
240 }
241
242 pub fn of(&self, e: &Expr) -> Option<&Type> {
244 self.types.get(&key(e))
245 }
246
247 pub fn operator(&self, e: &Expr) -> Option<&OperatorUse> {
249 self.operators.get(&key(e))
250 }
251
252 pub fn conversion(&self, e: &Expr) -> Option<&OperatorUse> {
254 self.conversions.get(&key(e))
255 }
256
257 pub fn assigned(&self, place: &Expr) -> Option<&OperatorUse> {
259 self.assigned.get(&key(place))
260 }
261
262 pub(super) fn set(&mut self, e: &Expr, t: Type) {
263 self.types.insert(key(e), t);
264 }
265
266 pub(super) fn set_operator(&mut self, e: &Expr, used: OperatorUse) {
267 self.operators.insert(key(e), used);
268 }
269
270 pub(super) fn set_assigned(&mut self, place: &Expr, used: OperatorUse) {
271 self.assigned.insert(key(place), used);
272 }
273
274 pub(super) fn set_conversion(&mut self, e: &Expr, used: OperatorUse) {
275 self.conversions.insert(key(e), used);
276 }
277}
278
279fn key(e: &Expr) -> usize {
281 std::ptr::from_ref(e) as usize
282}