Skip to main content

xidl_parser/hir/
type_dcl.rs

1use super::*;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Serialize, Deserialize, Clone)]
5pub struct TypeDcl {
6    pub decl: TypeDclInner,
7}
8
9#[derive(Debug, Serialize, Deserialize, Clone)]
10#[allow(clippy::large_enum_variant)]
11pub enum TypeDclInner {
12    ConstrTypeDcl(ConstrTypeDcl),
13    TypedefDcl(TypedefDcl),
14    NativeDcl(NativeDcl),
15}
16
17#[derive(Debug, Serialize, Deserialize, Clone)]
18pub struct TypedefDcl {
19    pub annotations: Vec<Annotation>,
20    pub ty: TypedefType,
21    pub decl: Vec<Declarator>,
22}
23
24#[derive(Debug, Serialize, Deserialize, Clone)]
25pub enum TypedefType {
26    TypeSpec(TypeSpec),
27    ConstrTypeDcl(ConstrTypeDcl),
28}
29
30#[derive(Debug, Serialize, Deserialize, Clone)]
31pub struct NativeDcl {
32    pub annotations: Vec<Annotation>,
33    pub decl: SimpleDeclarator,
34}
35
36impl From<crate::typed_ast::TypeDcl> for TypeDcl {
37    fn from(value: crate::typed_ast::TypeDcl) -> Self {
38        let annotations = expand_annotations(value.annotations);
39        let decl = match value.decl {
40            crate::typed_ast::TypeDclInner::ConstrTypeDcl(constr) => {
41                let mut decl: ConstrTypeDcl = constr.into();
42                match &mut decl {
43                    ConstrTypeDcl::StructForwardDcl(value) => value.annotations.extend(annotations),
44                    ConstrTypeDcl::StructDcl(value) => value.annotations.extend(annotations),
45                    ConstrTypeDcl::EnumDcl(value) => value.annotations.extend(annotations),
46                    ConstrTypeDcl::UnionForwardDcl(value) => value.annotations.extend(annotations),
47                    ConstrTypeDcl::UnionDef(value) => value.annotations.extend(annotations),
48                    ConstrTypeDcl::BitsetDcl(value) => value.annotations.extend(annotations),
49                    ConstrTypeDcl::BitmaskDcl(value) => value.annotations.extend(annotations),
50                }
51                TypeDclInner::ConstrTypeDcl(decl)
52            }
53            crate::typed_ast::TypeDclInner::TypedefDcl(typedef) => {
54                let mut decl: TypedefDcl = typedef.into();
55                decl.annotations.extend(annotations);
56                TypeDclInner::TypedefDcl(decl)
57            }
58            crate::typed_ast::TypeDclInner::NativeDcl(native) => {
59                let mut decl: NativeDcl = native.into();
60                decl.annotations.extend(annotations);
61                TypeDclInner::NativeDcl(decl)
62            }
63        };
64        Self { decl }
65    }
66}
67
68impl From<crate::typed_ast::TypeDclInner> for TypeDclInner {
69    fn from(value: crate::typed_ast::TypeDclInner) -> Self {
70        match value {
71            crate::typed_ast::TypeDclInner::ConstrTypeDcl(constr) => {
72                Self::ConstrTypeDcl(constr.into())
73            }
74            crate::typed_ast::TypeDclInner::TypedefDcl(typedef) => Self::TypedefDcl(typedef.into()),
75            crate::typed_ast::TypeDclInner::NativeDcl(native_dcl) => {
76                Self::NativeDcl(native_dcl.into())
77            }
78        }
79    }
80}
81
82impl From<crate::typed_ast::TypedefDcl> for TypedefDcl {
83    fn from(value: crate::typed_ast::TypedefDcl) -> Self {
84        let ty = match value.decl.ty {
85            crate::typed_ast::TypeDeclaratorInner::SimpleTypeSpec(simple) => {
86                TypedefType::TypeSpec(crate::typed_ast::TypeSpec::SimpleTypeSpec(simple).into())
87            }
88            crate::typed_ast::TypeDeclaratorInner::TemplateTypeSpec(template) => {
89                TypedefType::TypeSpec(crate::typed_ast::TypeSpec::TemplateTypeSpec(template).into())
90            }
91            crate::typed_ast::TypeDeclaratorInner::ConstrTypeDcl(constr) => {
92                TypedefType::ConstrTypeDcl(constr.into())
93            }
94        };
95        let decl = value
96            .decl
97            .decl
98            .0
99            .into_iter()
100            .map(|decl| match decl {
101                crate::typed_ast::AnyDeclarator::SimpleDeclarator(simple) => {
102                    Declarator::SimpleDeclarator(simple.into())
103                }
104                crate::typed_ast::AnyDeclarator::ArrayDeclarator(array) => {
105                    Declarator::ArrayDeclarator(array.into())
106                }
107            })
108            .collect();
109        Self {
110            annotations: vec![],
111            ty,
112            decl,
113        }
114    }
115}
116
117impl From<crate::typed_ast::NativeDcl> for NativeDcl {
118    fn from(value: crate::typed_ast::NativeDcl) -> Self {
119        Self {
120            annotations: vec![],
121            decl: value.decl.into(),
122        }
123    }
124}