Skip to main content

rajac_types/
type_.rs

1use crate::{
2    ArrayType, ClassType, PrimitiveType, TypeId, TypeVariable, WildcardBound, WildcardType,
3};
4use rajac_base::shared_string::SharedString;
5
6#[derive(Debug, Clone, PartialEq)]
7pub enum Type {
8    Primitive(PrimitiveType),
9    Class(ClassType),
10    Array(ArrayType),
11    TypeVariable(TypeVariable),
12    Wildcard(WildcardType),
13    Error,
14}
15
16impl Type {
17    pub fn primitive(primitive: PrimitiveType) -> Self {
18        Type::Primitive(primitive)
19    }
20
21    pub fn class(class: ClassType) -> Self {
22        Type::Class(class)
23    }
24
25    pub fn array(element_type: TypeId) -> Self {
26        Type::Array(ArrayType::new(element_type))
27    }
28
29    pub fn type_variable(name: SharedString) -> Self {
30        Type::TypeVariable(TypeVariable::new(name))
31    }
32
33    pub fn wildcard(bound: Option<WildcardBound>) -> Self {
34        Type::Wildcard(WildcardType { bound })
35    }
36
37    pub fn error() -> Self {
38        Type::Error
39    }
40
41    pub fn is_primitive(&self) -> bool {
42        matches!(self, Type::Primitive(_))
43    }
44
45    pub fn is_class(&self) -> bool {
46        matches!(self, Type::Class(_))
47    }
48
49    pub fn is_array(&self) -> bool {
50        matches!(self, Type::Array(_))
51    }
52
53    pub fn is_type_variable(&self) -> bool {
54        matches!(self, Type::TypeVariable(_))
55    }
56
57    pub fn is_wildcard(&self) -> bool {
58        matches!(self, Type::Wildcard(_))
59    }
60
61    pub fn is_error(&self) -> bool {
62        matches!(self, Type::Error)
63    }
64}