use super::{Aff, BasicMap, Context, DimType, Error, Id, LibISLError, Space};
use libc::uintptr_t;
use std::ffi::{CStr, CString};
use std::os::raw::c_char;
pub struct LocalSpace {
pub ptr: uintptr_t,
pub should_free_on_drop: bool,
}
extern "C" {
fn isl_local_space_add_dims(ls: uintptr_t, type_: i32, n: u32) -> uintptr_t;
fn isl_local_space_copy(ls: uintptr_t) -> uintptr_t;
fn isl_local_space_dim(ls: uintptr_t, type_: i32) -> i32;
fn isl_local_space_domain(ls: uintptr_t) -> uintptr_t;
fn isl_local_space_drop_dims(ls: uintptr_t, type_: i32, first: u32, n: u32) -> uintptr_t;
fn isl_local_space_dump(ls: uintptr_t) -> ();
fn isl_local_space_find_dim_by_name(ls: uintptr_t, type_: i32, name: *const c_char) -> i32;
fn isl_local_space_flatten_domain(ls: uintptr_t) -> uintptr_t;
fn isl_local_space_flatten_range(ls: uintptr_t) -> uintptr_t;
fn isl_local_space_free(ls: uintptr_t) -> uintptr_t;
fn isl_local_space_from_domain(ls: uintptr_t) -> uintptr_t;
fn isl_local_space_from_space(space: uintptr_t) -> uintptr_t;
fn isl_local_space_get_ctx(ls: uintptr_t) -> uintptr_t;
fn isl_local_space_get_dim_id(ls: uintptr_t, type_: i32, pos: u32) -> uintptr_t;
fn isl_local_space_get_dim_name(ls: uintptr_t, type_: i32, pos: u32) -> *const c_char;
fn isl_local_space_get_div(ls: uintptr_t, pos: i32) -> uintptr_t;
fn isl_local_space_get_space(ls: uintptr_t) -> uintptr_t;
fn isl_local_space_has_dim_id(ls: uintptr_t, type_: i32, pos: u32) -> i32;
fn isl_local_space_has_dim_name(ls: uintptr_t, type_: i32, pos: u32) -> i32;
fn isl_local_space_insert_dims(ls: uintptr_t, type_: i32, first: u32, n: u32) -> uintptr_t;
fn isl_local_space_intersect(ls1: uintptr_t, ls2: uintptr_t) -> uintptr_t;
fn isl_local_space_is_equal(ls1: uintptr_t, ls2: uintptr_t) -> i32;
fn isl_local_space_is_params(ls: uintptr_t) -> i32;
fn isl_local_space_is_set(ls: uintptr_t) -> i32;
fn isl_local_space_lifting(ls: uintptr_t) -> uintptr_t;
fn isl_local_space_range(ls: uintptr_t) -> uintptr_t;
fn isl_local_space_set_dim_id(ls: uintptr_t, type_: i32, pos: u32, id: uintptr_t) -> uintptr_t;
fn isl_local_space_set_dim_name(ls: uintptr_t, type_: i32, pos: u32, s: *const c_char)
-> uintptr_t;
fn isl_local_space_set_from_params(ls: uintptr_t) -> uintptr_t;
fn isl_local_space_set_tuple_id(ls: uintptr_t, type_: i32, id: uintptr_t) -> uintptr_t;
fn isl_local_space_wrap(ls: uintptr_t) -> uintptr_t;
}
impl LocalSpace {
pub fn add_dims(self, type_: DimType, n: u32) -> Result<LocalSpace, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let mut ls = ls;
ls.do_not_free_on_drop();
let ls = ls.ptr;
let type_ = type_.to_i32();
let isl_rs_result = unsafe { isl_local_space_add_dims(ls, type_, n) };
let isl_rs_result = LocalSpace { ptr: isl_rs_result,
should_free_on_drop: true };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn copy(&self) -> Result<LocalSpace, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let ls = ls.ptr;
let isl_rs_result = unsafe { isl_local_space_copy(ls) };
let isl_rs_result = LocalSpace { ptr: isl_rs_result,
should_free_on_drop: true };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn dim(&self, type_: DimType) -> Result<i32, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let ls = ls.ptr;
let type_ = type_.to_i32();
let isl_rs_result = unsafe { isl_local_space_dim(ls, type_) };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn domain(self) -> Result<LocalSpace, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let mut ls = ls;
ls.do_not_free_on_drop();
let ls = ls.ptr;
let isl_rs_result = unsafe { isl_local_space_domain(ls) };
let isl_rs_result = LocalSpace { ptr: isl_rs_result,
should_free_on_drop: true };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn drop_dims(self, type_: DimType, first: u32, n: u32) -> Result<LocalSpace, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let mut ls = ls;
ls.do_not_free_on_drop();
let ls = ls.ptr;
let type_ = type_.to_i32();
let isl_rs_result = unsafe { isl_local_space_drop_dims(ls, type_, first, n) };
let isl_rs_result = LocalSpace { ptr: isl_rs_result,
should_free_on_drop: true };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn dump(&self) -> Result<(), LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let ls = ls.ptr;
let isl_rs_result = unsafe { isl_local_space_dump(ls) };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn find_dim_by_name(&self, type_: DimType, name: &str) -> Result<i32, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let ls = ls.ptr;
let type_ = type_.to_i32();
let name = CString::new(name).unwrap();
let name = name.as_ptr();
let isl_rs_result = unsafe { isl_local_space_find_dim_by_name(ls, type_, name) };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn flatten_domain(self) -> Result<LocalSpace, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let mut ls = ls;
ls.do_not_free_on_drop();
let ls = ls.ptr;
let isl_rs_result = unsafe { isl_local_space_flatten_domain(ls) };
let isl_rs_result = LocalSpace { ptr: isl_rs_result,
should_free_on_drop: true };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn flatten_range(self) -> Result<LocalSpace, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let mut ls = ls;
ls.do_not_free_on_drop();
let ls = ls.ptr;
let isl_rs_result = unsafe { isl_local_space_flatten_range(ls) };
let isl_rs_result = LocalSpace { ptr: isl_rs_result,
should_free_on_drop: true };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn free(self) -> Result<LocalSpace, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let mut ls = ls;
ls.do_not_free_on_drop();
let ls = ls.ptr;
let isl_rs_result = unsafe { isl_local_space_free(ls) };
let isl_rs_result = LocalSpace { ptr: isl_rs_result,
should_free_on_drop: true };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn from_domain(self) -> Result<LocalSpace, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let mut ls = ls;
ls.do_not_free_on_drop();
let ls = ls.ptr;
let isl_rs_result = unsafe { isl_local_space_from_domain(ls) };
let isl_rs_result = LocalSpace { ptr: isl_rs_result,
should_free_on_drop: true };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn from_space(space: Space) -> Result<LocalSpace, LibISLError> {
let isl_rs_ctx = space.get_ctx();
let mut space = space;
space.do_not_free_on_drop();
let space = space.ptr;
let isl_rs_result = unsafe { isl_local_space_from_space(space) };
let isl_rs_result = LocalSpace { ptr: isl_rs_result,
should_free_on_drop: true };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn get_ctx(&self) -> Context {
let ls = self;
let ls = ls.ptr;
let isl_rs_result = unsafe { isl_local_space_get_ctx(ls) };
let isl_rs_result = Context { ptr: isl_rs_result,
should_free_on_drop: false };
isl_rs_result
}
pub fn get_dim_id(&self, type_: DimType, pos: u32) -> Result<Id, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let ls = ls.ptr;
let type_ = type_.to_i32();
let isl_rs_result = unsafe { isl_local_space_get_dim_id(ls, type_, pos) };
let isl_rs_result = Id { ptr: isl_rs_result,
should_free_on_drop: true };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn get_dim_name(&self, type_: DimType, pos: u32) -> Result<&str, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let ls = ls.ptr;
let type_ = type_.to_i32();
let isl_rs_result = unsafe { isl_local_space_get_dim_name(ls, type_, pos) };
let isl_rs_result = unsafe { CStr::from_ptr(isl_rs_result) };
let isl_rs_result = isl_rs_result.to_str().unwrap();
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn get_div(&self, pos: i32) -> Result<Aff, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let ls = ls.ptr;
let isl_rs_result = unsafe { isl_local_space_get_div(ls, pos) };
let isl_rs_result = Aff { ptr: isl_rs_result,
should_free_on_drop: true };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn get_space(&self) -> Result<Space, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let ls = ls.ptr;
let isl_rs_result = unsafe { isl_local_space_get_space(ls) };
let isl_rs_result = Space { ptr: isl_rs_result,
should_free_on_drop: true };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn has_dim_id(&self, type_: DimType, pos: u32) -> Result<bool, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let ls = ls.ptr;
let type_ = type_.to_i32();
let isl_rs_result = unsafe { isl_local_space_has_dim_id(ls, type_, pos) };
let isl_rs_result = match isl_rs_result {
0 => false,
1 => true,
_ => {
return Err(LibISLError::new(Error::Unknown, "Got isl_bool = -1"));
}
};
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn has_dim_name(&self, type_: DimType, pos: u32) -> Result<bool, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let ls = ls.ptr;
let type_ = type_.to_i32();
let isl_rs_result = unsafe { isl_local_space_has_dim_name(ls, type_, pos) };
let isl_rs_result = match isl_rs_result {
0 => false,
1 => true,
_ => {
return Err(LibISLError::new(Error::Unknown, "Got isl_bool = -1"));
}
};
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn insert_dims(self, type_: DimType, first: u32, n: u32)
-> Result<LocalSpace, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let mut ls = ls;
ls.do_not_free_on_drop();
let ls = ls.ptr;
let type_ = type_.to_i32();
let isl_rs_result = unsafe { isl_local_space_insert_dims(ls, type_, first, n) };
let isl_rs_result = LocalSpace { ptr: isl_rs_result,
should_free_on_drop: true };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn intersect(self, ls2: LocalSpace) -> Result<LocalSpace, LibISLError> {
let ls1 = self;
let isl_rs_ctx = ls1.get_ctx();
let mut ls1 = ls1;
ls1.do_not_free_on_drop();
let ls1 = ls1.ptr;
let mut ls2 = ls2;
ls2.do_not_free_on_drop();
let ls2 = ls2.ptr;
let isl_rs_result = unsafe { isl_local_space_intersect(ls1, ls2) };
let isl_rs_result = LocalSpace { ptr: isl_rs_result,
should_free_on_drop: true };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn is_equal(&self, ls2: &LocalSpace) -> Result<bool, LibISLError> {
let ls1 = self;
let isl_rs_ctx = ls1.get_ctx();
let ls1 = ls1.ptr;
let ls2 = ls2.ptr;
let isl_rs_result = unsafe { isl_local_space_is_equal(ls1, ls2) };
let isl_rs_result = match isl_rs_result {
0 => false,
1 => true,
_ => {
return Err(LibISLError::new(Error::Unknown, "Got isl_bool = -1"));
}
};
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn is_params(&self) -> Result<bool, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let ls = ls.ptr;
let isl_rs_result = unsafe { isl_local_space_is_params(ls) };
let isl_rs_result = match isl_rs_result {
0 => false,
1 => true,
_ => {
return Err(LibISLError::new(Error::Unknown, "Got isl_bool = -1"));
}
};
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn is_set(&self) -> Result<bool, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let ls = ls.ptr;
let isl_rs_result = unsafe { isl_local_space_is_set(ls) };
let isl_rs_result = match isl_rs_result {
0 => false,
1 => true,
_ => {
return Err(LibISLError::new(Error::Unknown, "Got isl_bool = -1"));
}
};
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn lifting(self) -> Result<BasicMap, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let mut ls = ls;
ls.do_not_free_on_drop();
let ls = ls.ptr;
let isl_rs_result = unsafe { isl_local_space_lifting(ls) };
let isl_rs_result = BasicMap { ptr: isl_rs_result,
should_free_on_drop: true };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn range(self) -> Result<LocalSpace, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let mut ls = ls;
ls.do_not_free_on_drop();
let ls = ls.ptr;
let isl_rs_result = unsafe { isl_local_space_range(ls) };
let isl_rs_result = LocalSpace { ptr: isl_rs_result,
should_free_on_drop: true };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn set_dim_id(self, type_: DimType, pos: u32, id: Id) -> Result<LocalSpace, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let mut ls = ls;
ls.do_not_free_on_drop();
let ls = ls.ptr;
let type_ = type_.to_i32();
let mut id = id;
id.do_not_free_on_drop();
let id = id.ptr;
let isl_rs_result = unsafe { isl_local_space_set_dim_id(ls, type_, pos, id) };
let isl_rs_result = LocalSpace { ptr: isl_rs_result,
should_free_on_drop: true };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn set_dim_name(self, type_: DimType, pos: u32, s: &str)
-> Result<LocalSpace, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let mut ls = ls;
ls.do_not_free_on_drop();
let ls = ls.ptr;
let type_ = type_.to_i32();
let s = CString::new(s).unwrap();
let s = s.as_ptr();
let isl_rs_result = unsafe { isl_local_space_set_dim_name(ls, type_, pos, s) };
let isl_rs_result = LocalSpace { ptr: isl_rs_result,
should_free_on_drop: true };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn set_from_params(self) -> Result<LocalSpace, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let mut ls = ls;
ls.do_not_free_on_drop();
let ls = ls.ptr;
let isl_rs_result = unsafe { isl_local_space_set_from_params(ls) };
let isl_rs_result = LocalSpace { ptr: isl_rs_result,
should_free_on_drop: true };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn set_tuple_id(self, type_: DimType, id: Id) -> Result<LocalSpace, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let mut ls = ls;
ls.do_not_free_on_drop();
let ls = ls.ptr;
let type_ = type_.to_i32();
let mut id = id;
id.do_not_free_on_drop();
let id = id.ptr;
let isl_rs_result = unsafe { isl_local_space_set_tuple_id(ls, type_, id) };
let isl_rs_result = LocalSpace { ptr: isl_rs_result,
should_free_on_drop: true };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn wrap(self) -> Result<LocalSpace, LibISLError> {
let ls = self;
let isl_rs_ctx = ls.get_ctx();
let mut ls = ls;
ls.do_not_free_on_drop();
let ls = ls.ptr;
let isl_rs_result = unsafe { isl_local_space_wrap(ls) };
let isl_rs_result = LocalSpace { ptr: isl_rs_result,
should_free_on_drop: true };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn do_not_free_on_drop(&mut self) {
self.should_free_on_drop = false;
}
}
impl Drop for LocalSpace {
fn drop(&mut self) {
if self.should_free_on_drop {
unsafe {
isl_local_space_free(self.ptr);
}
}
}
}