claw_resolver/
types.rs

1use ast::TypeId;
2use claw_ast as ast;
3
4use crate::imports::ImportTypeId;
5
6#[derive(Clone, Copy, Debug)]
7pub enum ResolvedType {
8    Primitive(ast::PrimitiveType),
9    Import(ImportTypeId),
10    Defined(TypeId),
11}
12
13impl From<TypeId> for ResolvedType {
14    fn from(value: TypeId) -> Self {
15        ResolvedType::Defined(value)
16    }
17}
18
19pub const RESOLVED_BOOL: ResolvedType = ResolvedType::Primitive(ast::PrimitiveType::Bool);
20
21impl std::fmt::Display for ResolvedType {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        match self {
24            ResolvedType::Primitive(p) => (p as &dyn std::fmt::Debug).fmt(f),
25            ResolvedType::Import(_) => write!(f, "imported type"),
26            ResolvedType::Defined(v) => (v as &dyn std::fmt::Debug).fmt(f),
27        }
28    }
29}
30
31impl ResolvedType {
32    pub fn type_eq(&self, other: &ResolvedType, comp: &ast::Component) -> bool {
33        match (*self, *other) {
34            // Both primitive
35            (ResolvedType::Primitive(left), ResolvedType::Primitive(right)) => left == right,
36            // Both valtype
37            (ResolvedType::Defined(left), ResolvedType::Defined(right)) => {
38                let l_valtype = comp.get_type(left);
39                let r_valtype = comp.get_type(right);
40                l_valtype.eq(r_valtype, comp)
41            }
42            // One primitive, other valtype
43            (ResolvedType::Primitive(p), ResolvedType::Defined(v))
44            | (ResolvedType::Defined(v), ResolvedType::Primitive(p)) => {
45                let valtype = comp.get_type(v);
46                match valtype {
47                    ast::ValType::Primitive(p2) => p == *p2,
48                    _ => false,
49                }
50            }
51            _ => todo!(),
52        }
53    }
54}