Skip to main content

rajac_ast/
ast_type.rs

1use rajac_base::shared_string::SharedString;
2use rajac_types::TypeId;
3
4#[derive(Debug, Clone, PartialEq)]
5pub struct AstTypeParam {
6    pub name: SharedString,
7    pub bounds: Vec<AstTypeId>,
8}
9
10#[derive(Debug, Clone, PartialEq)]
11pub enum AstType {
12    Simple {
13        name: SharedString,
14        type_args: Vec<AstTypeId>,
15        ty: TypeId,
16    },
17    Array {
18        element_type: AstTypeId,
19        dimensions: u32,
20        ty: TypeId,
21    },
22    Primitive {
23        kind: PrimitiveType,
24        ty: TypeId,
25    },
26    Wildcard {
27        bound: Option<WildcardBound>,
28        ty: TypeId,
29    },
30    Error,
31}
32
33#[derive(Debug, Clone, PartialEq)]
34pub enum PrimitiveType {
35    Byte,
36    Short,
37    Int,
38    Long,
39    Float,
40    Double,
41    Char,
42    Boolean,
43    Void,
44}
45
46#[derive(Debug, Clone, PartialEq)]
47pub enum WildcardBound {
48    Extends(AstTypeId),
49    Super(AstTypeId),
50}
51
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
53pub struct AstTypeId(pub u32);
54
55impl AstType {
56    pub fn simple(name: SharedString) -> Self {
57        Self::Simple {
58            name,
59            type_args: Vec::new(),
60            ty: TypeId::INVALID,
61        }
62    }
63
64    pub fn simple_with_args(name: SharedString, type_args: Vec<AstTypeId>) -> Self {
65        Self::Simple {
66            name,
67            type_args,
68            ty: TypeId::INVALID,
69        }
70    }
71
72    pub fn array(element_type: AstTypeId, dimensions: u32) -> Self {
73        Self::Array {
74            element_type,
75            dimensions,
76            ty: TypeId::INVALID,
77        }
78    }
79
80    pub fn primitive(kind: PrimitiveType) -> Self {
81        Self::Primitive {
82            kind,
83            ty: TypeId::INVALID,
84        }
85    }
86
87    pub fn wildcard(bound: Option<WildcardBound>) -> Self {
88        Self::Wildcard {
89            bound,
90            ty: TypeId::INVALID,
91        }
92    }
93
94    pub fn error() -> Self {
95        Self::Error
96    }
97
98    pub fn ty(&self) -> TypeId {
99        match self {
100            AstType::Simple { ty, .. } => *ty,
101            AstType::Array { ty, .. } => *ty,
102            AstType::Primitive { ty, .. } => *ty,
103            AstType::Wildcard { ty, .. } => *ty,
104            AstType::Error => TypeId::INVALID,
105        }
106    }
107
108    pub fn set_ty(&mut self, ty: TypeId) {
109        match self {
110            AstType::Simple { ty: field, .. } => *field = ty,
111            AstType::Array { ty: field, .. } => *field = ty,
112            AstType::Primitive { ty: field, .. } => *field = ty,
113            AstType::Wildcard { ty: field, .. } => *field = ty,
114            AstType::Error => {} // Can't set type for Error
115        }
116    }
117
118    pub fn is_simple(&self) -> bool {
119        matches!(self, AstType::Simple { .. })
120    }
121
122    pub fn is_array(&self) -> bool {
123        matches!(self, AstType::Array { .. })
124    }
125
126    pub fn is_primitive(&self) -> bool {
127        matches!(self, AstType::Primitive { .. })
128    }
129
130    pub fn is_wildcard(&self) -> bool {
131        matches!(self, AstType::Wildcard { .. })
132    }
133
134    pub fn is_error(&self) -> bool {
135        matches!(self, AstType::Error)
136    }
137}
138
139impl AstTypeId {
140    pub const INVALID: AstTypeId = AstTypeId(u32::MAX);
141}