use super::{Aff, ConstraintList, Context, DimType, Error, LibISLError, LocalSpace, Space, Val};
use libc::uintptr_t;
use std::ffi::CStr;
use std::os::raw::c_char;
pub struct Constraint {
pub ptr: uintptr_t,
pub should_free_on_drop: bool,
}
extern "C" {
fn isl_constraint_alloc_equality(ls: uintptr_t) -> uintptr_t;
fn isl_constraint_alloc_inequality(ls: uintptr_t) -> uintptr_t;
fn isl_constraint_cmp_last_non_zero(c1: uintptr_t, c2: uintptr_t) -> i32;
fn isl_constraint_copy(c: uintptr_t) -> uintptr_t;
fn isl_constraint_dim(constraint: uintptr_t, type_: i32) -> i32;
fn isl_constraint_dump(c: uintptr_t) -> ();
fn isl_constraint_free(c: uintptr_t) -> uintptr_t;
fn isl_constraint_get_aff(constraint: uintptr_t) -> uintptr_t;
fn isl_constraint_get_bound(constraint: uintptr_t, type_: i32, pos: i32) -> uintptr_t;
fn isl_constraint_get_coefficient_val(constraint: uintptr_t, type_: i32, pos: i32)
-> uintptr_t;
fn isl_constraint_get_constant_val(constraint: uintptr_t) -> uintptr_t;
fn isl_constraint_get_ctx(c: uintptr_t) -> uintptr_t;
fn isl_constraint_get_dim_name(constraint: uintptr_t, type_: i32, pos: u32) -> *const c_char;
fn isl_constraint_get_div(constraint: uintptr_t, pos: i32) -> uintptr_t;
fn isl_constraint_get_local_space(constraint: uintptr_t) -> uintptr_t;
fn isl_constraint_get_space(constraint: uintptr_t) -> uintptr_t;
fn isl_constraint_involves_dims(constraint: uintptr_t, type_: i32, first: u32, n: u32) -> i32;
fn isl_constraint_is_div_constraint(constraint: uintptr_t) -> i32;
fn isl_constraint_is_equal(constraint1: uintptr_t, constraint2: uintptr_t) -> i32;
fn isl_constraint_is_equality(constraint: uintptr_t) -> i32;
fn isl_constraint_is_lower_bound(constraint: uintptr_t, type_: i32, pos: u32) -> i32;
fn isl_constraint_is_upper_bound(constraint: uintptr_t, type_: i32, pos: u32) -> i32;
fn isl_constraint_negate(constraint: uintptr_t) -> uintptr_t;
fn isl_constraint_plain_cmp(c1: uintptr_t, c2: uintptr_t) -> i32;
fn isl_constraint_set_coefficient_si(constraint: uintptr_t, type_: i32, pos: i32, v: i32)
-> uintptr_t;
fn isl_constraint_set_coefficient_val(constraint: uintptr_t, type_: i32, pos: i32,
v: uintptr_t)
-> uintptr_t;
fn isl_constraint_set_constant_si(constraint: uintptr_t, v: i32) -> uintptr_t;
fn isl_constraint_set_constant_val(constraint: uintptr_t, v: uintptr_t) -> uintptr_t;
fn isl_constraint_to_list(el: uintptr_t) -> uintptr_t;
}
impl Constraint {
pub fn alloc_equality(ls: LocalSpace) -> Result<Constraint, LibISLError> {
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_constraint_alloc_equality(ls) };
let isl_rs_result = Constraint { 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 alloc_inequality(ls: LocalSpace) -> Result<Constraint, LibISLError> {
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_constraint_alloc_inequality(ls) };
let isl_rs_result = Constraint { 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 cmp_last_non_zero(&self, c2: &Constraint) -> Result<i32, LibISLError> {
let c1 = self;
let isl_rs_ctx = c1.get_ctx();
let c1 = c1.ptr;
let c2 = c2.ptr;
let isl_rs_result = unsafe { isl_constraint_cmp_last_non_zero(c1, c2) };
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<Constraint, LibISLError> {
let c = self;
let isl_rs_ctx = c.get_ctx();
let c = c.ptr;
let isl_rs_result = unsafe { isl_constraint_copy(c) };
let isl_rs_result = Constraint { 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 constraint = self;
let isl_rs_ctx = constraint.get_ctx();
let constraint = constraint.ptr;
let type_ = type_.to_i32();
let isl_rs_result = unsafe { isl_constraint_dim(constraint, 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 dump(&self) -> Result<(), LibISLError> {
let c = self;
let isl_rs_ctx = c.get_ctx();
let c = c.ptr;
let isl_rs_result = unsafe { isl_constraint_dump(c) };
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<Constraint, LibISLError> {
let c = self;
let isl_rs_ctx = c.get_ctx();
let mut c = c;
c.do_not_free_on_drop();
let c = c.ptr;
let isl_rs_result = unsafe { isl_constraint_free(c) };
let isl_rs_result = Constraint { 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_aff(&self) -> Result<Aff, LibISLError> {
let constraint = self;
let isl_rs_ctx = constraint.get_ctx();
let constraint = constraint.ptr;
let isl_rs_result = unsafe { isl_constraint_get_aff(constraint) };
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_bound(&self, type_: DimType, pos: i32) -> Result<Aff, LibISLError> {
let constraint = self;
let isl_rs_ctx = constraint.get_ctx();
let constraint = constraint.ptr;
let type_ = type_.to_i32();
let isl_rs_result = unsafe { isl_constraint_get_bound(constraint, type_, 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_coefficient_val(&self, type_: DimType, pos: i32) -> Result<Val, LibISLError> {
let constraint = self;
let isl_rs_ctx = constraint.get_ctx();
let constraint = constraint.ptr;
let type_ = type_.to_i32();
let isl_rs_result = unsafe { isl_constraint_get_coefficient_val(constraint, type_, pos) };
let isl_rs_result = Val { 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_constant_val(&self) -> Result<Val, LibISLError> {
let constraint = self;
let isl_rs_ctx = constraint.get_ctx();
let constraint = constraint.ptr;
let isl_rs_result = unsafe { isl_constraint_get_constant_val(constraint) };
let isl_rs_result = Val { 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 c = self;
let c = c.ptr;
let isl_rs_result = unsafe { isl_constraint_get_ctx(c) };
let isl_rs_result = Context { ptr: isl_rs_result,
should_free_on_drop: false };
isl_rs_result
}
pub fn get_dim_name(&self, type_: DimType, pos: u32) -> Result<&str, LibISLError> {
let constraint = self;
let isl_rs_ctx = constraint.get_ctx();
let constraint = constraint.ptr;
let type_ = type_.to_i32();
let isl_rs_result = unsafe { isl_constraint_get_dim_name(constraint, 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 constraint = self;
let isl_rs_ctx = constraint.get_ctx();
let constraint = constraint.ptr;
let isl_rs_result = unsafe { isl_constraint_get_div(constraint, 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_local_space(&self) -> Result<LocalSpace, LibISLError> {
let constraint = self;
let isl_rs_ctx = constraint.get_ctx();
let constraint = constraint.ptr;
let isl_rs_result = unsafe { isl_constraint_get_local_space(constraint) };
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_space(&self) -> Result<Space, LibISLError> {
let constraint = self;
let isl_rs_ctx = constraint.get_ctx();
let constraint = constraint.ptr;
let isl_rs_result = unsafe { isl_constraint_get_space(constraint) };
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 involves_dims(&self, type_: DimType, first: u32, n: u32) -> Result<bool, LibISLError> {
let constraint = self;
let isl_rs_ctx = constraint.get_ctx();
let constraint = constraint.ptr;
let type_ = type_.to_i32();
let isl_rs_result = unsafe { isl_constraint_involves_dims(constraint, type_, first, n) };
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_div_constraint(&self) -> Result<bool, LibISLError> {
let constraint = self;
let isl_rs_ctx = constraint.get_ctx();
let constraint = constraint.ptr;
let isl_rs_result = unsafe { isl_constraint_is_div_constraint(constraint) };
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_equal(&self, constraint2: &Constraint) -> Result<i32, LibISLError> {
let constraint1 = self;
let isl_rs_ctx = constraint1.get_ctx();
let constraint1 = constraint1.ptr;
let constraint2 = constraint2.ptr;
let isl_rs_result = unsafe { isl_constraint_is_equal(constraint1, constraint2) };
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_equality(&self) -> Result<bool, LibISLError> {
let constraint = self;
let isl_rs_ctx = constraint.get_ctx();
let constraint = constraint.ptr;
let isl_rs_result = unsafe { isl_constraint_is_equality(constraint) };
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_lower_bound(&self, type_: DimType, pos: u32) -> Result<bool, LibISLError> {
let constraint = self;
let isl_rs_ctx = constraint.get_ctx();
let constraint = constraint.ptr;
let type_ = type_.to_i32();
let isl_rs_result = unsafe { isl_constraint_is_lower_bound(constraint, 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 is_upper_bound(&self, type_: DimType, pos: u32) -> Result<bool, LibISLError> {
let constraint = self;
let isl_rs_ctx = constraint.get_ctx();
let constraint = constraint.ptr;
let type_ = type_.to_i32();
let isl_rs_result = unsafe { isl_constraint_is_upper_bound(constraint, 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 negate(self) -> Result<Constraint, LibISLError> {
let constraint = self;
let isl_rs_ctx = constraint.get_ctx();
let mut constraint = constraint;
constraint.do_not_free_on_drop();
let constraint = constraint.ptr;
let isl_rs_result = unsafe { isl_constraint_negate(constraint) };
let isl_rs_result = Constraint { 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 plain_cmp(&self, c2: &Constraint) -> Result<i32, LibISLError> {
let c1 = self;
let isl_rs_ctx = c1.get_ctx();
let c1 = c1.ptr;
let c2 = c2.ptr;
let isl_rs_result = unsafe { isl_constraint_plain_cmp(c1, c2) };
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_coefficient_si(self, type_: DimType, pos: i32, v: i32)
-> Result<Constraint, LibISLError> {
let constraint = self;
let isl_rs_ctx = constraint.get_ctx();
let mut constraint = constraint;
constraint.do_not_free_on_drop();
let constraint = constraint.ptr;
let type_ = type_.to_i32();
let isl_rs_result = unsafe { isl_constraint_set_coefficient_si(constraint, type_, pos, v) };
let isl_rs_result = Constraint { 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_coefficient_val(self, type_: DimType, pos: i32, v: Val)
-> Result<Constraint, LibISLError> {
let constraint = self;
let isl_rs_ctx = constraint.get_ctx();
let mut constraint = constraint;
constraint.do_not_free_on_drop();
let constraint = constraint.ptr;
let type_ = type_.to_i32();
let mut v = v;
v.do_not_free_on_drop();
let v = v.ptr;
let isl_rs_result =
unsafe { isl_constraint_set_coefficient_val(constraint, type_, pos, v) };
let isl_rs_result = Constraint { 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_constant_si(self, v: i32) -> Result<Constraint, LibISLError> {
let constraint = self;
let isl_rs_ctx = constraint.get_ctx();
let mut constraint = constraint;
constraint.do_not_free_on_drop();
let constraint = constraint.ptr;
let isl_rs_result = unsafe { isl_constraint_set_constant_si(constraint, v) };
let isl_rs_result = Constraint { 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_constant_val(self, v: Val) -> Result<Constraint, LibISLError> {
let constraint = self;
let isl_rs_ctx = constraint.get_ctx();
let mut constraint = constraint;
constraint.do_not_free_on_drop();
let constraint = constraint.ptr;
let mut v = v;
v.do_not_free_on_drop();
let v = v.ptr;
let isl_rs_result = unsafe { isl_constraint_set_constant_val(constraint, v) };
let isl_rs_result = Constraint { 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 to_list(self) -> Result<ConstraintList, LibISLError> {
let el = self;
let isl_rs_ctx = el.get_ctx();
let mut el = el;
el.do_not_free_on_drop();
let el = el.ptr;
let isl_rs_result = unsafe { isl_constraint_to_list(el) };
let isl_rs_result = ConstraintList { 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 Constraint {
fn drop(&mut self) {
if self.should_free_on_drop {
unsafe {
isl_constraint_free(self.ptr);
}
}
}
}