Function tir_core::utils::op_dyn_cast
source ยท pub fn op_dyn_cast<T: ?Sized + 'static>(op: OpRef) -> Option<Rc<RefCell<T>>>
Expand description
Cast an operation to a registered trait
Rust does not allow trait upcasting (at least for now). To be able to cast
from dyn Op
to user-defined trait, the trait must be globally registered.
That is easily done with tir_macros::op_implements
attribute:
use tir_core::Terminator;
#[tir_macros::op_implements(dialect = test)]
impl Terminator for TestOp {}
And later users can dynamically query known traits and perform generic transformations or analysis on operations.
Example:
use tir_core::{OpRef, utils::op_dyn_cast, Terminator};
fn test(op: OpRef) {
if let Some(op) = op_dyn_cast::<dyn Terminator>(op) {
// do smth useful
}
}