[rust] Cleanup visitors
Joe Savona committed
Aug 14, 2023 at 09:54 UTC
57f648009cea9ce84ed3480ba5ca50794e680b02
3 files changed
+18
-27
compiler/forget/crates/forget_hir/src/instruction.rs
+9
-14
@@ -14,7 +14,7 @@ pub struct Instruction {
14
}
15
16
impl Instruction {
17
- pub fn each_identifier_store<F>(&mut self, mut f: F) -> ()
17
+ pub fn each_lvalue<F>(&mut self, mut f: F) -> ()
18
where
19
F: FnMut(&mut IdentifierOperand) -> (),
20
{
@@ -29,7 +29,7 @@ impl Instruction {
29
f(&mut instr.lvalue.identifier);
30
}
31
InstructionValue::Destructure(instr) => {
32
- instr.pattern.each_operand(f);
32
+ instr.pattern.each_operand(&mut f);
33
}
34
InstructionValue::Array(_)
35
| InstructionValue::Binary(_)
@@ -42,9 +42,10 @@ impl Instruction {
42
| InstructionValue::JSXElement(_)
43
| InstructionValue::Tombstone => {}
44
}
45
+ f(&mut self.lvalue);
46
}
47
47
- pub fn try_each_identifier_store<F, E>(&mut self, mut f: F) -> Result<(), E>
48
+ pub fn try_each_lvalue<F, E>(&mut self, mut f: F) -> Result<(), E>
49
where
50
F: FnMut(&mut IdentifierOperand) -> Result<(), E>,
51
{
@@ -58,7 +59,7 @@ impl Instruction {
59
InstructionValue::StoreLocal(instr) => {
60
f(&mut instr.lvalue.identifier)?;
61
}
61
- InstructionValue::Destructure(instr) => instr.pattern.try_each_operand(f)?,
62
+ InstructionValue::Destructure(instr) => instr.pattern.try_each_operand(&mut f)?,
63
InstructionValue::Array(_)
64
| InstructionValue::Binary(_)
65
| InstructionValue::Call(_)
@@ -70,17 +71,11 @@ impl Instruction {
71
| InstructionValue::JSXElement(_)
72
| InstructionValue::Tombstone => {}
73
}
74
+ f(&mut self.lvalue)?;
75
Ok(())
76
}
77
76
- pub fn each_identifier_load<F>(&mut self, f: F) -> ()
77
- where
78
- F: FnMut(&mut IdentifierOperand) -> (),
79
- {
80
- self.each_operand(f);
81
- }
82
-
83
- pub fn each_operand<F>(&mut self, mut f: F) -> ()
78
+ pub fn each_rvalue<F>(&mut self, mut f: F) -> ()
79
where
80
F: FnMut(&mut IdentifierOperand) -> (),
81
{
@@ -281,7 +276,7 @@ pub enum DestructurePattern {
276
}
277
278
impl DestructurePattern {
284
- pub fn try_each_operand<E, F>(&mut self, mut f: F) -> Result<(), E>
279
+ pub fn try_each_operand<E, F>(&mut self, f: &mut F) -> Result<(), E>
280
where
281
F: FnMut(&mut IdentifierOperand) -> Result<(), E>,
282
{
@@ -308,7 +303,7 @@ impl DestructurePattern {
303
}
304
Ok(())
305
}
311
- pub fn each_operand<F>(&mut self, mut f: F) -> ()
306
+ pub fn each_operand<F>(&mut self, f: &mut F) -> ()
307
where
308
F: FnMut(&mut IdentifierOperand) -> (),
309
{
compiler/forget/crates/forget_ssa/src/eliminate_redundant_phis.rs
+2
-2
@@ -70,8 +70,8 @@ pub fn eliminate_redundant_phis(env: &Environment, fun: &mut Function) {
70
71
for instr_ix in block.instructions.iter() {
72
let instr = &mut hir.instructions[usize::from(*instr_ix)];
73
- instr.each_identifier_store(|store| rewrite(&rewrites, &mut store.identifier));
74
- instr.each_identifier_load(|load| rewrite(&rewrites, &mut load.identifier));
73
+ instr.each_lvalue(|lvalue| rewrite(&rewrites, &mut lvalue.identifier));
74
+ instr.each_rvalue(|rvalue| rewrite(&rewrites, &mut rvalue.identifier));
75
76
if let InstructionValue::Function(fun) = &mut instr.value {
77
for context_identifier in &mut fun.lowered_function.context {
compiler/forget/crates/forget_ssa/src/enter.rs
+7
-11
@@ -1,8 +1,7 @@
1
use std::cell::RefCell;
2
-use std::process::id;
2
use std::rc::Rc;
3
5
-use forget_diagnostics::{invariant, Diagnostic};
4
+use forget_diagnostics::Diagnostic;
5
use forget_hir::{
6
BasicBlock, BlockId, BlockRewriter, BlockRewriterAction, Blocks, Environment, Function,
7
Identifier, IdentifierData, IdentifierId, IdentifierOperand, InstructionValue, MutableRange,
@@ -52,9 +51,8 @@ fn visit_instructions<'e>(
51
builder.start_block(&block);
52
for instr_ix in &block.instructions {
53
let instr = &mut instructions[usize::from(*instr_ix)];
55
- instr.each_identifier_load(|load| builder.visit_load(load));
56
- instr.try_each_identifier_store(|store| builder.visit_store(store))?;
57
- builder.visit_store(&mut instr.lvalue)?;
54
+ instr.each_rvalue(|rvalue| builder.visit_load(rvalue));
55
+ instr.try_each_lvalue(|lvalue| builder.visit_store(lvalue))?;
56
57
if let InstructionValue::Function(fun) = &mut instr.value {
58
// Lookup each of the context variables referenced in the function
@@ -158,14 +156,12 @@ impl<'e> Builder<'e> {
156
157
fn visit_store(&mut self, lvalue: &mut IdentifierOperand) -> Result<(), Diagnostic> {
158
let old_identifier = &lvalue.identifier;
161
- // TODO: use Result (?)
162
- invariant(!self.unknown.contains(&old_identifier.id), || {
163
- Diagnostic::invariant(
159
+ if self.unknown.contains(&old_identifier.id) {
160
+ return Err(Diagnostic::invariant(
161
"EnterSSA: Expected identifier to be defined before being used",
162
None,
166
- )
167
- })?;
168
-
163
+ ));
164
+ }
165
if self.context.contains(&old_identifier.id) {
166
let new_identifier = self.get_id_at(self.current, old_identifier);
167
lvalue.identifier = new_identifier;