use cranelift_codegen::ir::{DataFlowGraph, Inst, InstructionData, Type, Value};
pub trait InstructionContext {
fn data(&self) -> InstructionData;
fn args(&self) -> &[Value];
fn type_of(&self, v: Value) -> Option<Type>;
fn controlling_type(&self) -> Option<Type>;
}
pub struct DfgInstructionContext<'a>(Inst, &'a DataFlowGraph);
impl<'a> DfgInstructionContext<'a> {
pub fn new(inst: Inst, dfg: &'a DataFlowGraph) -> Self {
Self(inst, dfg)
}
}
impl InstructionContext for DfgInstructionContext<'_> {
fn data(&self) -> InstructionData {
self.1.insts[self.0]
}
fn args(&self) -> &[Value] {
self.1.inst_args(self.0)
}
fn type_of(&self, v: Value) -> Option<Type> {
Some(self.1.value_type(v))
}
fn controlling_type(&self) -> Option<Type> {
Some(self.1.ctrl_typevar(self.0))
}
}