Skip to main content

rspirv/sr/
types.rs

1use crate::spirv;
2
3use super::{storage::Token, Constant, Decoration};
4
5#[derive(Clone, Debug)]
6pub struct StructMember {
7    pub token: Token<Type>,
8    pub decorations: Vec<Decoration>,
9}
10
11impl StructMember {
12    pub fn new(ty: Token<Type>) -> Self {
13        StructMember {
14            token: ty,
15            decorations: Vec::new(),
16        }
17    }
18}
19
20include!("autogen_types.rs");
21
22impl PartialEq for Type {
23    fn eq(&self, other: &Self) -> bool {
24        use Type::*;
25        match (self, other) {
26            (Bool, Bool) => true,
27            (
28                Int {
29                    width: sw,
30                    signedness: ss,
31                },
32                Int {
33                    width: ow,
34                    signedness: os,
35                },
36            ) => sw == ow && ss == os,
37            (
38                Float {
39                    width: sw,
40                    floating_point_encoding: se,
41                },
42                Float {
43                    width: ow,
44                    floating_point_encoding: oe,
45                },
46            ) => sw == ow && se == oe,
47            (
48                Vector {
49                    component_type: st,
50                    component_count: sc,
51                },
52                Vector {
53                    component_type: ot,
54                    component_count: oc,
55                },
56            ) => st == ot && sc == oc,
57            (
58                Matrix {
59                    column_type: st,
60                    column_count: sc,
61                },
62                Matrix {
63                    column_type: ot,
64                    column_count: oc,
65                },
66            ) => st == ot && sc == oc,
67            _ => false,
68        }
69    }
70}
71
72impl Type {
73    pub fn is_numerical_type(&self) -> bool {
74        matches!(self, Type::Int { .. } | Type::Float { .. })
75    }
76
77    pub fn is_scalar_type(&self) -> bool {
78        match self {
79            Type::Bool => true,
80            _ => self.is_numerical_type(),
81        }
82    }
83
84    pub fn is_aggregate_type(&self) -> bool {
85        matches!(
86            self,
87            Type::Struct { .. } | Type::Array { .. } | Type::RuntimeArray { .. }
88        )
89    }
90
91    pub fn is_composite_type(&self) -> bool {
92        match self {
93            Type::Vector { .. } | Type::Matrix { .. } => true,
94            _ => self.is_aggregate_type(),
95        }
96    }
97}