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}
33
34#[derive(Clone, Debug, Serialize, Deserialize)]
35pub struct SequenceType {
36    pub ty: Box<TypeSpec>,
37    pub len: Option<PositiveIntConst>,
38}
39
40#[derive(Clone, Debug, Serialize, Deserialize)]
41pub struct MapType {
42    pub key: Box<TypeSpec>,
43    pub value: Box<TypeSpec>,
44    pub len: Option<PositiveIntConst>,
45}
46
47#[derive(Clone, Debug, Serialize, Deserialize)]
48pub struct StringType {
49    pub bound: Option<PositiveIntConst>,
50}
51
52#[derive(Clone, Debug, Serialize, Deserialize)]
53pub struct WideStringType {
54    pub bound: Option<PositiveIntConst>,
55}
56
57#[derive(Clone, Debug, Serialize, Deserialize)]
58pub struct FixedPtType {
59    pub integer: PositiveIntConst,
60    pub fraction: PositiveIntConst,
61}
62
63#[derive(Clone, Debug, Serialize, Deserialize)]
64pub enum IntegerType {
65    Char,
66    UChar,
67    U8,
68    U16,
69    U32,
70    U64,
71    I8,
72    I16,
73    I32,
74    I64,
75}
76
77impl From<crate::typed_ast::TypeSpec> for TypeSpec {
78    fn from(value: crate::typed_ast::TypeSpec) -> Self {
79        match value {
80            crate::typed_ast::TypeSpec::SimpleTypeSpec(simple_type_spec) => {
81                Self::SimpleTypeSpec(simple_type_spec.into())
82            }
83            crate::typed_ast::TypeSpec::TemplateTypeSpec(template_type_spec) => {
84                Self::TemplateTypeSpec(template_type_spec.into())
85            }
86        }
87    }
88}
89
90impl From<crate::typed_ast::SimpleTypeSpec> for SimpleTypeSpec {
91    fn from(ty: crate::typed_ast::SimpleTypeSpec) -> Self {
92        match ty {
93            crate::typed_ast::SimpleTypeSpec::BaseTypeSpec(base_type_spec) => {
94                match base_type_spec {
95                    crate::typed_ast::BaseTypeSpec::IntegerType(integer_type) => {
96                        Self::IntegerType(integer_type.into())
97                    }
98                    crate::typed_ast::BaseTypeSpec::FloatingPtType(_) => Self::FloatingPtType,
99                    crate::typed_ast::BaseTypeSpec::CharType(_) => Self::CharType,
100                    crate::typed_ast::BaseTypeSpec::WideCharType(_) => Self::WideCharType,
101                    crate::typed_ast::BaseTypeSpec::BooleanType(_) => Self::Boolean,
102                    crate::typed_ast::BaseTypeSpec::OctetType(_) => {
103                        Self::IntegerType(IntegerType::U8)
104                    }
105                    crate::typed_ast::BaseTypeSpec::AnyType(_) => Self::AnyType,
106                    crate::typed_ast::BaseTypeSpec::ObjectType(_) => Self::ObjectType,
107                    crate::typed_ast::BaseTypeSpec::ValueBaseType(_) => Self::ValueBaseType,
108                }
109            }
110            crate::typed_ast::SimpleTypeSpec::ScopedName(scoped_name) => {
111                Self::ScopedName(scoped_name.into())
112            }
113        }
114    }
115}
116
117impl From<crate::typed_ast::IntegerType> for IntegerType {
118    fn from(value: crate::typed_ast::IntegerType) -> Self {
119        match value {
120            crate::typed_ast::IntegerType::SignedInt(signed_int) => match signed_int {
121                crate::typed_ast::SignedInt::SignedShortInt(_) => Self::I16,
122                crate::typed_ast::SignedInt::SignedLongInt(_) => Self::I32,
123                crate::typed_ast::SignedInt::SignedLongLongInt(_) => Self::I64,
124                crate::typed_ast::SignedInt::SignedTinyInt(_) => Self::I8,
125            },
126            crate::typed_ast::IntegerType::UnsignedInt(unsigned_int) => match unsigned_int {
127                crate::typed_ast::UnsignedInt::UnsignedShortInt(_) => Self::U16,
128                crate::typed_ast::UnsignedInt::UnsignedLongInt(_) => Self::U32,
129                crate::typed_ast::UnsignedInt::UnsignedLongLongInt(_) => Self::U64,
130                crate::typed_ast::UnsignedInt::UnsignedTinyInt(_) => Self::U8,
131            },
132        }
133    }
134}
135
136impl From<crate::typed_ast::TemplateTypeSpec> for TemplateTypeSpec {
137    fn from(value: crate::typed_ast::TemplateTypeSpec) -> Self {
138        match value {
139            crate::typed_ast::TemplateTypeSpec::SequenceType(sequence_type) => {
140                Self::SequenceType(sequence_type.into())
141            }
142            crate::typed_ast::TemplateTypeSpec::StringType(string_type) => {
143                Self::StringType(string_type.into())
144            }
145            crate::typed_ast::TemplateTypeSpec::WideStringType(wide_string_type) => {
146                Self::WideStringType(wide_string_type.into())
147            }
148            crate::typed_ast::TemplateTypeSpec::FixedPtType(fixed_pt_type) => {
149                Self::FixedPtType(fixed_pt_type.into())
150            }
151            crate::typed_ast::TemplateTypeSpec::MapType(map_type) => Self::MapType(map_type.into()),
152        }
153    }
154}
155
156impl From<crate::typed_ast::SequenceType> for SequenceType {
157    fn from(value: crate::typed_ast::SequenceType) -> Self {
158        Self {
159            ty: Box::new((*value.ty).into()),
160            len: value.len.map(Into::into),
161        }
162    }
163}
164
165impl From<crate::typed_ast::MapType> for MapType {
166    fn from(value: crate::typed_ast::MapType) -> Self {
167        Self {
168            key: Box::new((*value.key).into()),
169            value: Box::new((*value.value).into()),
170            len: value.len.map(Into::into),
171        }
172    }
173}
174
175impl From<crate::typed_ast::StringType> for StringType {
176    fn from(value: crate::typed_ast::StringType) -> Self {
177        Self {
178            bound: value.bound.map(Into::into),
179        }
180    }
181}
182
183impl From<crate::typed_ast::WideStringType> for WideStringType {
184    fn from(value: crate::typed_ast::WideStringType) -> Self {
185        Self {
186            bound: value.bound.map(Into::into),
187        }
188    }
189}
190
191impl From<crate::typed_ast::FixedPtType> for FixedPtType {
192    fn from(value: crate::typed_ast::FixedPtType) -> Self {
193        Self {
194            integer: value.integer.into(),
195            fraction: value.fraction.into(),
196        }
197    }
198}