use crate::ir::{Block, BlockRef};
use mlir_sys::{
MlirRegion, mlirRegionAppendOwnedBlock, mlirRegionGetFirstBlock,
mlirRegionInsertOwnedBlockAfter, mlirRegionInsertOwnedBlockBefore,
};
pub trait RegionLike<'c, 'a>: 'a {
fn to_raw(&self) -> MlirRegion;
fn first_block(&self) -> Option<BlockRef<'c, 'a>> {
unsafe {
let block = mlirRegionGetFirstBlock(self.to_raw());
if block.ptr.is_null() {
None
} else {
Some(BlockRef::from_raw(block))
}
}
}
fn insert_block_after(&self, one: BlockRef<'c, 'a>, other: Block<'c>) -> BlockRef<'c, 'a> {
unsafe {
let r#ref = BlockRef::from_raw(other.to_raw());
mlirRegionInsertOwnedBlockAfter(self.to_raw(), one.to_raw(), other.into_raw());
r#ref
}
}
fn insert_block_before(&self, one: BlockRef<'c, 'a>, other: Block<'c>) -> BlockRef<'c, 'a> {
unsafe {
let r#ref = BlockRef::from_raw(other.to_raw());
mlirRegionInsertOwnedBlockBefore(self.to_raw(), one.to_raw(), other.into_raw());
r#ref
}
}
fn append_block(&self, block: Block<'c>) -> BlockRef<'c, 'a> {
unsafe {
let r#ref = BlockRef::from_raw(block.to_raw());
mlirRegionAppendOwnedBlock(self.to_raw(), block.into_raw());
r#ref
}
}
}