Skip to main content

xidl_parser/hir/
type_dcl.rs

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