Skip to main content

melior/ir/
affine_map.rs

1use crate::{
2    context::{Context, ContextRef},
3    utility::print_callback,
4};
5use mlir_sys::{
6    mlirAffineMapDump, mlirAffineMapEqual, mlirAffineMapGetContext, mlirAffineMapPrint,
7    MlirAffineMap,
8};
9use std::{
10    ffi::c_void,
11    fmt::{self, Debug, Display, Formatter},
12    marker::PhantomData,
13};
14
15/// An affine map.
16#[derive(Clone, Copy)]
17pub struct AffineMap<'c> {
18    raw: MlirAffineMap,
19    _context: PhantomData<&'c Context>,
20}
21
22impl<'c> AffineMap<'c> {
23    /// Returns a context.
24    pub fn context(&self) -> ContextRef<'c> {
25        unsafe { ContextRef::from_raw(mlirAffineMapGetContext(self.raw)) }
26    }
27
28    /// Dumps an affine map.
29    pub fn dump(&self) {
30        unsafe { mlirAffineMapDump(self.raw) }
31    }
32
33    /// Creates an affine map from a raw object.
34    ///
35    /// # Safety
36    ///
37    /// A raw object must be valid.
38    pub unsafe fn from_raw(raw: MlirAffineMap) -> Self {
39        Self {
40            raw,
41            _context: Default::default(),
42        }
43    }
44}
45
46impl PartialEq for AffineMap<'_> {
47    fn eq(&self, other: &Self) -> bool {
48        unsafe { mlirAffineMapEqual(self.raw, other.raw) }
49    }
50}
51
52impl Eq for AffineMap<'_> {}
53
54impl Display for AffineMap<'_> {
55    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
56        let mut data = (formatter, Ok(()));
57
58        unsafe {
59            mlirAffineMapPrint(
60                self.raw,
61                Some(print_callback),
62                &mut data as *mut _ as *mut c_void,
63            );
64        }
65
66        data.1
67    }
68}
69
70impl Debug for AffineMap<'_> {
71    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
72        Display::fmt(self, formatter)
73    }
74}