waffle/src/ir/value.rs

67 lines
2 KiB
Rust
Raw Normal View History

2022-11-02 16:07:07 -05:00
use super::{Block, Type, Value};
2022-11-01 22:51:18 -05:00
use crate::Operator;
2022-11-02 16:07:07 -05:00
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
2022-11-01 22:51:18 -05:00
pub enum ValueDef {
BlockParam(Block, usize, Type),
Operator(Operator, Vec<Value>, Vec<Type>),
PickOutput(Value, usize, Type),
Alias(Value),
Placeholder(Type),
2022-11-02 16:07:07 -05:00
#[default]
None,
2022-11-01 22:51:18 -05:00
}
impl ValueDef {
2022-11-18 01:56:44 -06:00
pub fn ty(&self) -> Option<Type> {
match self {
&ValueDef::BlockParam(_, _, ty) => Some(ty),
&ValueDef::Operator(_, _, ref tys) if tys.len() == 0 => None,
&ValueDef::Operator(_, _, ref tys) if tys.len() == 1 => Some(tys[0]),
&ValueDef::PickOutput(_, _, ty) => Some(ty),
&ValueDef::Placeholder(ty) => Some(ty),
_ => None,
}
2022-11-20 15:54:27 -06:00
}
pub fn tys(&self) -> &[Type] {
match self {
&ValueDef::Operator(_, _, ref tys) => &tys[..],
&ValueDef::BlockParam(_, _, ref ty)
| &ValueDef::PickOutput(_, _, ref ty)
| &ValueDef::Placeholder(ref ty) => std::slice::from_ref(ty),
_ => &[],
}
2022-11-18 01:56:44 -06:00
}
2022-11-01 22:51:18 -05:00
pub fn visit_uses<F: FnMut(Value)>(&self, mut f: F) {
match self {
&ValueDef::BlockParam { .. } => {}
&ValueDef::Operator(_, ref args, _) => {
for &arg in args {
f(arg);
}
}
&ValueDef::PickOutput(from, ..) => f(from),
&ValueDef::Alias(value) => f(value),
&ValueDef::Placeholder(_) => {}
2022-11-02 16:07:07 -05:00
&ValueDef::None => panic!(),
2022-11-01 22:51:18 -05:00
}
}
pub fn update_uses<F: FnMut(&mut Value)>(&mut self, mut f: F) {
match self {
&mut ValueDef::BlockParam { .. } => {}
&mut ValueDef::Operator(_, ref mut args, _) => {
for arg in args {
f(arg);
}
}
&mut ValueDef::PickOutput(ref mut from, ..) => f(from),
&mut ValueDef::Alias(ref mut value) => f(value),
&mut ValueDef::Placeholder(_) => {}
2022-11-02 16:07:07 -05:00
&mut ValueDef::None => panic!(),
2022-11-01 22:51:18 -05:00
}
}
}