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 annotations: Vec<Annotation>,
7    pub decl: TypeDclInner,
8}
9
10#[derive(Debug, Serialize, Deserialize, Clone)]
11#[allow(clippy::large_enum_variant)]
12pub enum TypeDclInner {
13    ConstrTypeDcl(ConstrTypeDcl),
14    TypedefDcl(TypedefDcl),
15    NativeDcl(NativeDcl),
16}
17
18#[derive(Debug, Serialize, Deserialize, Clone)]
19pub struct TypedefDcl {
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 decl: SimpleDeclarator,
33}
34
35impl From<crate::typed_ast::TypeDcl> for TypeDcl {
36    fn from(value: crate::typed_ast::TypeDcl) -> Self {
37        Self {
38            annotations: expand_annotations(value.annotations),
39            decl: value.decl.into(),
40        }
41    }
42}
43
44impl From<crate::typed_ast::TypeDclInner> for TypeDclInner {
45    fn from(value: crate::typed_ast::TypeDclInner) -> Self {
46        match value {
47            crate::typed_ast::TypeDclInner::ConstrTypeDcl(constr) => {
48                Self::ConstrTypeDcl(constr.into())
49            }
50            crate::typed_ast::TypeDclInner::TypedefDcl(typedef) => Self::TypedefDcl(typedef.into()),
51            crate::typed_ast::TypeDclInner::NativeDcl(native_dcl) => {
52                Self::NativeDcl(native_dcl.into())
53            }
54        }
55    }
56}
57
58impl From<crate::typed_ast::TypedefDcl> for TypedefDcl {
59    fn from(value: crate::typed_ast::TypedefDcl) -> Self {
60        let ty = match value.decl.ty {
61            crate::typed_ast::TypeDeclaratorInner::SimpleTypeSpec(simple) => {
62                TypedefType::TypeSpec(crate::typed_ast::TypeSpec::SimpleTypeSpec(simple).into())
63            }
64            crate::typed_ast::TypeDeclaratorInner::TemplateTypeSpec(template) => {
65                TypedefType::TypeSpec(crate::typed_ast::TypeSpec::TemplateTypeSpec(template).into())
66            }
67            crate::typed_ast::TypeDeclaratorInner::ConstrTypeDcl(constr) => {
68                TypedefType::ConstrTypeDcl(constr.into())
69            }
70        };
71        let decl = value
72            .decl
73            .decl
74            .0
75            .into_iter()
76            .map(|decl| match decl {
77                crate::typed_ast::AnyDeclarator::SimpleDeclarator(simple) => {
78                    Declarator::SimpleDeclarator(simple.into())
79                }
80                crate::typed_ast::AnyDeclarator::ArrayDeclarator(array) => {
81                    Declarator::ArrayDeclarator(array.into())
82                }
83            })
84            .collect();
85        Self { ty, decl }
86    }
87}
88
89impl From<crate::typed_ast::NativeDcl> for NativeDcl {
90    fn from(value: crate::typed_ast::NativeDcl) -> Self {
91        Self {
92            decl: value.decl.into(),
93        }
94    }
95}