Skip to main content

xidl_parser/hir/
types.rs

1use serde::{Deserialize, Serialize};
2
3use super::*;
4
5#[derive(Clone, Debug, Serialize, Deserialize)]
6#[allow(clippy::large_enum_variant)]
7pub enum TypeSpec {
8    SimpleTypeSpec(SimpleTypeSpec),
9    TemplateTypeSpec(TemplateTypeSpec),
10}
11
12#[derive(Clone, Debug, Serialize, Deserialize)]
13pub enum SimpleTypeSpec {
14    IntegerType(IntegerType),
15    FloatingPtType,
16    CharType,
17    WideCharType,
18    Boolean,
19    AnyType,
20    ObjectType,
21    ValueBaseType,
22    ScopedName(ScopedName),
23}
24
25#[derive(Clone, Debug, Serialize, Deserialize)]
26pub enum TemplateTypeSpec {
27    SequenceType(SequenceType),
28    StringType(StringType),
29    WideStringType(WideStringType),
30    FixedPtType(FixedPtType),
31    MapType(MapType),
32    TemplateType(TemplateType),
33}
34
35#[derive(Clone, Debug, Serialize, Deserialize)]
36pub struct SequenceType {
37    pub ty: Box<TypeSpec>,
38    pub len: Option<PositiveIntConst>,
39}
40
41#[derive(Clone, Debug, Serialize, Deserialize)]
42pub struct MapType {
43    pub key: Box<TypeSpec>,
44    pub value: Box<TypeSpec>,
45    pub len: Option<PositiveIntConst>,
46}
47
48#[derive(Clone, Debug, Serialize, Deserialize)]
49pub struct TemplateType {
50    pub ident: String,
51    pub args: Vec<TypeSpec>,
52}
53
54#[derive(Clone, Debug, Serialize, Deserialize)]
55pub struct StringType {
56    pub bound: Option<PositiveIntConst>,
57}
58
59#[derive(Clone, Debug, Serialize, Deserialize)]
60pub struct WideStringType {
61    pub bound: Option<PositiveIntConst>,
62}
63
64#[derive(Clone, Debug, Serialize, Deserialize)]
65pub struct FixedPtType {
66    pub integer: PositiveIntConst,
67    pub fraction: PositiveIntConst,
68}
69
70#[derive(Clone, Debug, Serialize, Deserialize)]
71pub enum IntegerType {
72    Char,
73    UChar,
74    U8,
75    U16,
76    U32,
77    U64,
78    I8,
79    I16,
80    I32,
81    I64,
82}
83
84impl From<crate::typed_ast::TypeSpec> for TypeSpec {
85    fn from(value: crate::typed_ast::TypeSpec) -> Self {
86        match value {
87            crate::typed_ast::TypeSpec::SimpleTypeSpec(simple_type_spec) => {
88                Self::SimpleTypeSpec(simple_type_spec.into())
89            }
90            crate::typed_ast::TypeSpec::TemplateTypeSpec(template_type_spec) => {
91                Self::TemplateTypeSpec(template_type_spec.into())
92            }
93        }
94    }
95}
96
97impl From<crate::typed_ast::SimpleTypeSpec> for SimpleTypeSpec {
98    fn from(ty: crate::typed_ast::SimpleTypeSpec) -> Self {
99        match ty {
100            crate::typed_ast::SimpleTypeSpec::BaseTypeSpec(base_type_spec) => {
101                match base_type_spec {
102                    crate::typed_ast::BaseTypeSpec::IntegerType(integer_type) => {
103                        Self::IntegerType(integer_type.into())
104                    }
105                    crate::typed_ast::BaseTypeSpec::FloatingPtType(_) => Self::FloatingPtType,
106                    crate::typed_ast::BaseTypeSpec::CharType(_) => Self::CharType,
107                    crate::typed_ast::BaseTypeSpec::WideCharType(_) => Self::WideCharType,
108                    crate::typed_ast::BaseTypeSpec::BooleanType(_) => Self::Boolean,
109                    crate::typed_ast::BaseTypeSpec::OctetType(_) => {
110                        Self::IntegerType(IntegerType::U8)
111                    }
112                    crate::typed_ast::BaseTypeSpec::AnyType(_) => Self::AnyType,
113                    crate::typed_ast::BaseTypeSpec::ObjectType(_) => Self::ObjectType,
114                    crate::typed_ast::BaseTypeSpec::ValueBaseType(_) => Self::ValueBaseType,
115                }
116            }
117            crate::typed_ast::SimpleTypeSpec::ScopedName(scoped_name) => {
118                Self::ScopedName(scoped_name.into())
119            }
120        }
121    }
122}
123
124impl From<crate::typed_ast::IntegerType> for IntegerType {
125    fn from(value: crate::typed_ast::IntegerType) -> Self {
126        match value {
127            crate::typed_ast::IntegerType::SignedInt(signed_int) => match signed_int {
128                crate::typed_ast::SignedInt::SignedShortInt(_) => Self::I16,
129                crate::typed_ast::SignedInt::SignedLongInt(_) => Self::I32,
130                crate::typed_ast::SignedInt::SignedLongLongInt(_) => Self::I64,
131                crate::typed_ast::SignedInt::SignedTinyInt(_) => Self::I8,
132            },
133            crate::typed_ast::IntegerType::UnsignedInt(unsigned_int) => match unsigned_int {
134                crate::typed_ast::UnsignedInt::UnsignedShortInt(_) => Self::U16,
135                crate::typed_ast::UnsignedInt::UnsignedLongInt(_) => Self::U32,
136                crate::typed_ast::UnsignedInt::UnsignedLongLongInt(_) => Self::U64,
137                crate::typed_ast::UnsignedInt::UnsignedTinyInt(_) => Self::U8,
138            },
139        }
140    }
141}
142
143impl From<crate::typed_ast::TemplateTypeSpec> for TemplateTypeSpec {
144    fn from(value: crate::typed_ast::TemplateTypeSpec) -> Self {
145        match value {
146            crate::typed_ast::TemplateTypeSpec::SequenceType(sequence_type) => {
147                Self::SequenceType(sequence_type.into())
148            }
149            crate::typed_ast::TemplateTypeSpec::StringType(string_type) => {
150                Self::StringType(string_type.into())
151            }
152            crate::typed_ast::TemplateTypeSpec::WideStringType(wide_string_type) => {
153                Self::WideStringType(wide_string_type.into())
154            }
155            crate::typed_ast::TemplateTypeSpec::FixedPtType(fixed_pt_type) => {
156                Self::FixedPtType(fixed_pt_type.into())
157            }
158            crate::typed_ast::TemplateTypeSpec::MapType(map_type) => Self::MapType(map_type.into()),
159            crate::typed_ast::TemplateTypeSpec::TemplateType(template_type) => {
160                Self::TemplateType(template_type.into())
161            }
162        }
163    }
164}
165
166impl From<crate::typed_ast::SequenceType> for SequenceType {
167    fn from(value: crate::typed_ast::SequenceType) -> Self {
168        Self {
169            ty: Box::new((*value.ty).into()),
170            len: value.len.map(Into::into),
171        }
172    }
173}
174
175impl From<crate::typed_ast::MapType> for MapType {
176    fn from(value: crate::typed_ast::MapType) -> Self {
177        Self {
178            key: Box::new((*value.key).into()),
179            value: Box::new((*value.value).into()),
180            len: value.len.map(Into::into),
181        }
182    }
183}
184
185impl From<crate::typed_ast::TemplateType> for TemplateType {
186    fn from(value: crate::typed_ast::TemplateType) -> Self {
187        Self {
188            ident: value.ident.0,
189            args: value.args.into_iter().map(Into::into).collect(),
190        }
191    }
192}
193
194impl From<crate::typed_ast::StringType> for StringType {
195    fn from(value: crate::typed_ast::StringType) -> Self {
196        Self {
197            bound: value.bound.map(Into::into),
198        }
199    }
200}
201
202impl From<crate::typed_ast::WideStringType> for WideStringType {
203    fn from(value: crate::typed_ast::WideStringType) -> Self {
204        Self {
205            bound: value.bound.map(Into::into),
206        }
207    }
208}
209
210impl From<crate::typed_ast::FixedPtType> for FixedPtType {
211    fn from(value: crate::typed_ast::FixedPtType) -> Self {
212        Self {
213            integer: value.integer.into(),
214            fraction: value.fraction.into(),
215        }
216    }
217}