use melior::{
StringRef,
ir::{
Block, BlockRef, Operation, OperationRef, Region, RegionLike, RegionRef,
operation::OperationLike,
},
};
use mlir_sys::MlirStringRef;
use std::{
ffi::c_void,
fmt::{self, Formatter},
};
pub trait FromRaw<RawT> {
unsafe fn from_raw(raw: RawT) -> Self;
}
#[allow(dead_code)]
pub(crate) unsafe extern "C" fn print_callback(string: MlirStringRef, data: *mut c_void) {
unsafe {
let (formatter, result) = &mut *(data as *mut (&mut Formatter, fmt::Result));
if result.is_err() {
return;
}
*result = (|| {
write!(
formatter,
"{}",
StringRef::from_raw(string)
.as_str()
.map_err(|_| fmt::Error)?
)
})();
}
}
#[macro_export]
macro_rules! ident {
($ctx:expr, $name:expr) => {{
let ctx = $ctx;
melior::ir::Identifier::new(unsafe { ctx.to_ref() }, $name)
}};
}
pub trait IntoRef<RefType> {
fn into_ref(self) -> RefType;
}
macro_rules! impl_into_ref {
($owned:ty, $ref:ty) => {
impl<'c, 'a> IntoRef<$ref> for $owned {
#[inline]
fn into_ref(self) -> $ref {
unsafe { <$ref>::from_raw(self.to_raw()) }
}
}
};
}
impl_into_ref!(Block<'c>, BlockRef<'c, 'a>);
impl_into_ref!(Region<'c>, RegionRef<'c, 'a>);
impl_into_ref!(Operation<'c>, OperationRef<'c, 'a>);
pub trait IsA: Sized {
#[inline]
fn isa<Out: TryFrom<Self>>(self) -> bool {
Out::try_from(self).is_ok()
}
}
impl<T> IsA for T {}