1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use crate::spirv;

use super::storage::Token;

/// Represents a SPIR-V constant.
#[derive(Clone, Debug, PartialEq)]
pub enum Constant {
    Bool(bool),
    // TODO: Change to arbitrary width datatype
    Int(i32),
    UInt(u32),
    Float(f32),
    Composite(Vec<Token<Constant>>),
    Null,
    Sampler {
        addressing_mode: spirv::SamplerAddressingMode,
        normalized: bool,
        filter_mode: spirv::SamplerFilterMode,
    },
    SpecBool(bool),
    SpecInt(i32),
    SpecUInt(u32),
    SpecFloat(f32),
    SpecComposite(Vec<Token<Constant>>),
    SpecOp(spirv::Op, Vec<Token<Constant>>),
}

impl Constant {
    pub fn is_bool_constant(&self) -> bool {
        matches!(self, Constant::Bool { .. } | Constant::SpecBool { .. })
    }

    pub fn is_int_constant(&self) -> bool {
        matches!(self, Constant::Int { .. } | Constant::SpecInt { .. })
    }

    pub fn is_uint_constant(&self) -> bool {
        matches!(self, Constant::UInt { .. } | Constant::SpecUInt { .. })
    }

    pub fn is_float_constant(&self) -> bool {
        matches!(self, Constant::Float { .. } | Constant::SpecFloat { .. })
    }

    pub fn is_composite_constant(&self) -> bool {
        matches!(
            self,
            Constant::Composite { .. } | Constant::SpecComposite { .. }
        )
    }

    pub fn is_null_constant(&self) -> bool {
        matches!(self, Constant::Null { .. })
    }

    pub fn is_sampler_constant(&self) -> bool {
        matches!(self, Constant::Sampler { .. })
    }

    pub fn is_spec_constant(&self) -> bool {
        matches!(
            self,
            Constant::SpecBool { .. }
                | Constant::SpecInt { .. }
                | Constant::SpecUInt { .. }
                | Constant::SpecFloat { .. }
                | Constant::SpecComposite { .. }
                | Constant::SpecOp { .. }
        )
    }

    pub fn is_spec_op_constant(&self) -> bool {
        matches!(self, Constant::SpecOp { .. })
    }
}