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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use std::fmt;
use std::collections::HashSet;

use super::ir::{AccessType, Meta, VariableName};

/// A variable use (a variable, component or signal read or write).
#[derive(Clone, Hash, PartialEq, Eq)]
pub struct VariableUse {
    meta: Meta,
    name: VariableName,
    access: Vec<AccessType>,
}

impl VariableUse {
    pub fn new(meta: &Meta, name: &VariableName, access: &[AccessType]) -> VariableUse {
        VariableUse { meta: meta.clone(), name: name.clone(), access: access.to_owned() }
    }

    pub fn meta(&self) -> &Meta {
        &self.meta
    }

    pub fn name(&self) -> &VariableName {
        &self.name
    }

    pub fn access(&self) -> &Vec<AccessType> {
        &self.access
    }
}

impl fmt::Display for VariableUse {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.name)?;
        for access in &self.access {
            write!(f, "{}", access)?;
        }
        Ok(())
    }
}

pub type VariableUses = HashSet<VariableUse>;

pub trait VariableMeta {
    /// Compute variables read/written by the node. Must be called before either
    /// of the getters are called. To avoid interior mutability this needs to be
    /// called again whenever the node is mutated in a way that may invalidate
    /// the cached variable use.
    fn cache_variable_use(&mut self);

    /// Get the set of variables read by the IR node.
    #[must_use]
    fn locals_read(&self) -> &VariableUses;

    /// Get the set of variables written by the IR node.
    #[must_use]
    fn locals_written(&self) -> &VariableUses;

    /// Get the set of signals read by the IR node. Note that this does not
    /// include signals belonging to sub-components.
    #[must_use]
    fn signals_read(&self) -> &VariableUses;

    /// Get the set of signals written by the IR node. Note that this does not
    /// include signals belonging to sub-components.
    #[must_use]
    fn signals_written(&self) -> &VariableUses;

    /// Get the set of components read by the IR node. Note that a component
    /// read is typically a signal read for a signal exported by the component.
    #[must_use]
    fn components_read(&self) -> &VariableUses;

    /// Get the set of components written by the IR node. Note that a component
    /// write may either be a component initialization, or a signal write for a
    /// signal exported by the component.
    #[must_use]
    fn components_written(&self) -> &VariableUses;

    /// Get the set of variables read by the IR node. Note that this is simply
    /// the union of all locals, signals, and components read by the node.
    #[must_use]
    fn variables_read<'a>(&'a self) -> Box<dyn Iterator<Item = &'a VariableUse> + 'a> {
        let locals_read = self.locals_read().iter();
        let signals_read = self.signals_read().iter();
        let components_read = self.components_read().iter();
        Box::new(locals_read.chain(signals_read).chain(components_read))
    }

    /// Get the set of variables written by the IR node. Note that this is
    /// simply the union of all locals, signals, and components written.
    #[must_use]
    fn variables_written<'a>(&'a self) -> Box<dyn Iterator<Item = &'a VariableUse> + 'a> {
        let locals_written = self.locals_written().iter();
        let signals_written = self.signals_written().iter();
        let components_written = self.components_written().iter();
        Box::new(locals_written.chain(signals_written).chain(components_written))
    }

    /// Get the set of variables either read or written by the IR node.
    #[must_use]
    fn variables_used<'a>(&'a self) -> Box<dyn Iterator<Item = &'a VariableUse> + 'a> {
        Box::new(self.variables_read().chain(self.variables_written()))
    }
}

#[derive(Default, Clone)]
pub struct VariableKnowledge {
    locals_read: Option<VariableUses>,
    locals_written: Option<VariableUses>,
    signals_read: Option<VariableUses>,
    signals_written: Option<VariableUses>,
    components_read: Option<VariableUses>,
    components_written: Option<VariableUses>,
}

impl VariableKnowledge {
    #[must_use]
    pub fn new() -> VariableKnowledge {
        VariableKnowledge::default()
    }

    pub fn set_locals_read(&mut self, uses: &VariableUses) -> &mut VariableKnowledge {
        self.locals_read = Some(uses.clone());
        self
    }

    pub fn set_locals_written(&mut self, uses: &VariableUses) -> &mut VariableKnowledge {
        self.locals_written = Some(uses.clone());
        self
    }

    pub fn set_signals_read(&mut self, uses: &VariableUses) -> &mut VariableKnowledge {
        self.signals_read = Some(uses.clone());
        self
    }

    pub fn set_signals_written(&mut self, uses: &VariableUses) -> &mut VariableKnowledge {
        self.signals_written = Some(uses.clone());
        self
    }

    pub fn set_components_read(&mut self, uses: &VariableUses) -> &mut VariableKnowledge {
        self.components_read = Some(uses.clone());
        self
    }

    pub fn set_components_written(&mut self, uses: &VariableUses) -> &mut VariableKnowledge {
        self.components_written = Some(uses.clone());
        self
    }

    #[must_use]
    pub fn locals_read(&self) -> &VariableUses {
        self.locals_read.as_ref().expect("variable knowledge must be initialized before it is read")
    }

    #[must_use]
    pub fn locals_written(&self) -> &VariableUses {
        self.locals_written
            .as_ref()
            .expect("variable knowledge must be initialized before it is read")
    }

    #[must_use]
    pub fn signals_read(&self) -> &VariableUses {
        self.signals_read
            .as_ref()
            .expect("variable knowledge must be initialized before it is read")
    }

    #[must_use]
    pub fn signals_written(&self) -> &VariableUses {
        self.signals_written
            .as_ref()
            .expect("variable knowledge must be initialized before it is read")
    }

    #[must_use]
    pub fn components_read(&self) -> &VariableUses {
        self.components_read
            .as_ref()
            .expect("variable knowledge must be initialized before it is read")
    }

    #[must_use]
    pub fn components_written(&self) -> &VariableUses {
        self.components_written
            .as_ref()
            .expect("variable knowledge must be initialized before it is read")
    }

    #[must_use]
    pub fn variables_read<'a>(&'a self) -> Box<dyn Iterator<Item = &'a VariableUse> + 'a> {
        let locals_read = self.locals_read().iter();
        let signals_read = self.signals_read().iter();
        let components_read = self.components_read().iter();
        Box::new(locals_read.chain(signals_read).chain(components_read))
    }

    #[must_use]
    pub fn variables_written<'a>(&'a self) -> Box<dyn Iterator<Item = &'a VariableUse> + 'a> {
        let locals_written = self.locals_written().iter();
        let signals_written = self.signals_written().iter();
        let components_written = self.components_written().iter();
        Box::new(locals_written.chain(signals_written).chain(components_written))
    }

    #[must_use]
    pub fn variables_used<'a>(&'a self) -> Box<dyn Iterator<Item = &'a VariableUse> + 'a> {
        let variables_read = self.variables_read();
        let variables_written = self.variables_written();
        Box::new(variables_read.chain(variables_written))
    }
}