yog_script_analyze/typeck/types.rs
1//! Each expression's type, as the checker found it: what a compiler needs
2//! to write the code (a local's declaration, a value boxed, a switch's
3//! result) and an editor to show.
4
5use std::collections::HashMap;
6
7use super::model::Type;
8use crate::ast::Expr;
9
10/// The types of a checked file's expressions. An expression is known by
11/// its node in the file's tree — which, once checked, isn't changed — so
12/// two that share a place (a macro's output, all at its call) stay apart.
13#[derive(Debug, Default)]
14pub struct Types(HashMap<usize, Type>);
15
16impl Types {
17 /// `e`'s type, if it was checked.
18 pub fn of(&self, e: &Expr) -> Option<&Type> {
19 self.0.get(&key(e))
20 }
21
22 pub(super) fn set(&mut self, e: &Expr, t: Type) {
23 self.0.insert(key(e), t);
24 }
25}
26
27/// A node's address: its identity in the tree.
28fn key(e: &Expr) -> usize {
29 std::ptr::from_ref(e) as usize
30}