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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use super::{HandleSet, ModuleMap};
use crate::{Handle, UniqueArena};

pub struct TypeTracer<'a> {
    pub types: &'a UniqueArena<crate::Type>,
    pub types_used: &'a mut HandleSet<crate::Type>,
}

impl<'a> TypeTracer<'a> {
    pub fn trace_type(&mut self, ty: Handle<crate::Type>) {
        let mut work_list = vec![ty];
        while let Some(ty) = work_list.pop() {
            // If we've already seen this type, no need to traverse further.
            if !self.types_used.insert(ty) {
                continue;
            }

            use crate::TypeInner as Ti;
            match self.types[ty].inner {
                // Types that do not contain handles.
                Ti::Scalar { .. }
                | Ti::Vector { .. }
                | Ti::Matrix { .. }
                | Ti::Atomic { .. }
                | Ti::ValuePointer { .. }
                | Ti::Image { .. }
                | Ti::Sampler { .. }
                | Ti::AccelerationStructure
                | Ti::RayQuery => {}

                // Types that do contain handles.
                Ti::Pointer { base, space: _ } => work_list.push(base),
                Ti::Array {
                    base,
                    size: _,
                    stride: _,
                } => work_list.push(base),
                Ti::Struct {
                    ref members,
                    span: _,
                } => {
                    work_list.extend(members.iter().map(|m| m.ty));
                }
                Ti::BindingArray { base, size: _ } => work_list.push(base),
            }
        }
    }
}

impl ModuleMap {
    pub fn adjust_type(&self, ty: &mut crate::Type) {
        let adjust = |ty: &mut Handle<crate::Type>| self.types.adjust(ty);

        use crate::TypeInner as Ti;
        match ty.inner {
            // Types that do not contain handles.
            Ti::Scalar { .. }
            | Ti::Vector { .. }
            | Ti::Matrix { .. }
            | Ti::Atomic { .. }
            | Ti::ValuePointer { .. }
            | Ti::Image { .. }
            | Ti::Sampler { .. }
            | Ti::AccelerationStructure
            | Ti::RayQuery => {}

            // Types that do contain handles.
            Ti::Pointer {
                ref mut base,
                space: _,
            } => adjust(base),
            Ti::Array {
                ref mut base,
                size: _,
                stride: _,
            } => adjust(base),
            Ti::Struct {
                ref mut members,
                span: _,
            } => {
                for member in members {
                    self.types.adjust(&mut member.ty);
                }
            }
            Ti::BindingArray {
                ref mut base,
                size: _,
            } => {
                adjust(base);
            }
        };
    }
}