use alloc::{string::String, vec::Vec};
use hir_lang::{BinderId, Def, ExprId, ItemId, PatId, PathId, Prim};
use intern_lang::Lookup;
use crate::{
diag::Diagnostic,
profile::Profile,
types::{TypeDisplay, TypeId, Types},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Coercion {
Widen,
ToAny,
WrapNullable,
Null,
Subclass,
RecordWidth,
Unsize,
Reborrow,
AutoRef,
AutoDeref,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum OpClass {
Int(Prim),
Float(Prim),
Bool,
Char,
Str,
NullCheck,
Dynamic,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Stats {
pub steps: u64,
pub type_nodes: usize,
}
pub(crate) const NONE: u32 = u32::MAX;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Typed {
pub(crate) types: Types,
pub(crate) exprs: Vec<u32>,
pub(crate) pats: Vec<u32>,
pub(crate) binders: Vec<u32>,
pub(crate) path_defs: Vec<(PathId, Def)>,
pub(crate) methods: Vec<(ExprId, ItemId)>,
pub(crate) coercions: Vec<(ExprId, Coercion, TypeId)>,
pub(crate) op_classes: Vec<(ExprId, OpClass)>,
pub(crate) deferred: Vec<ExprId>,
pub(crate) diagnostics: Vec<Diagnostic>,
pub(crate) profile: Profile,
pub(crate) stats: Stats,
}
fn lookup<K: Ord + Copy, V: Copy>(table: &[(K, V)], key: K) -> Option<V> {
table
.binary_search_by(|(k, _)| k.cmp(&key))
.ok()
.and_then(|i| table.get(i))
.map(|&(_, v)| v)
}
fn dense(table: &[u32], index: usize) -> Option<TypeId> {
table
.get(index)
.copied()
.filter(|&raw| raw != NONE)
.map(TypeId::from_raw)
}
impl Typed {
#[must_use]
pub fn expr(&self, id: ExprId) -> Option<TypeId> {
dense(&self.exprs, id.index())
}
#[must_use]
pub fn pat(&self, id: PatId) -> Option<TypeId> {
dense(&self.pats, id.index())
}
#[must_use]
pub fn binder(&self, id: BinderId) -> Option<TypeId> {
dense(&self.binders, id.index())
}
#[must_use]
pub fn types(&self) -> &Types {
&self.types
}
#[must_use]
pub fn diagnostics(&self) -> &[Diagnostic] {
&self.diagnostics
}
#[must_use]
pub fn is_ok(&self) -> bool {
self.diagnostics.is_empty()
}
#[must_use]
pub fn path_def(&self, path: PathId) -> Option<Def> {
lookup(&self.path_defs, path)
}
#[must_use]
pub fn method(&self, call: ExprId) -> Option<ItemId> {
lookup(&self.methods, call)
}
#[must_use]
pub fn coercion(&self, expr: ExprId) -> Option<(Coercion, TypeId)> {
self.coercions
.binary_search_by(|(e, _, _)| e.cmp(&expr))
.ok()
.and_then(|i| self.coercions.get(i))
.map(|&(_, c, t)| (c, t))
}
#[must_use]
pub fn op_class(&self, op: ExprId) -> Option<OpClass> {
lookup(&self.op_classes, op)
}
#[must_use]
pub fn deferred_matches(&self) -> &[ExprId] {
&self.deferred
}
#[must_use]
pub fn profile(&self) -> &Profile {
&self.profile
}
#[must_use]
pub fn stats(&self) -> Stats {
self.stats
}
#[must_use]
pub fn display<'a, L: Lookup>(&'a self, ty: TypeId, names: &'a L) -> TypeDisplay<'a, L> {
self.types.display(ty, names)
}
#[must_use]
pub fn message<L: Lookup>(&self, diagnostic: &Diagnostic, names: &L) -> String {
let mut out = String::new();
diagnostic.kind.render(&self.types, names, &mut out);
out
}
}