use crate::ast::Span;
#[cfg(feature = "mlir")]
use melior::{
dialect::DialectRegistry,
ir::{Location, Module},
utility::register_all_dialects,
Context,
};
#[cfg(feature = "mlir")]
pub struct MlirContext {
context: Context,
}
#[cfg(feature = "mlir")]
impl MlirContext {
pub fn new() -> Self {
let registry = DialectRegistry::new();
register_all_dialects(®istry);
let context = Context::new();
context.append_dialect_registry(®istry);
context.load_all_available_dialects();
Self { context }
}
pub fn context(&self) -> &Context {
&self.context
}
pub fn create_module(&self, name: &str) -> Module {
Module::new(self.unknown_location(name))
}
pub fn location_from_span(&self, span: &Span) -> Location {
Location::unknown(&self.context)
}
pub fn unknown_location(&self, _identifier: &str) -> Location {
Location::unknown(&self.context)
}
}
#[cfg(feature = "mlir")]
impl Default for MlirContext {
fn default() -> Self {
Self::new()
}
}
#[cfg(not(feature = "mlir"))]
pub struct MlirContext {
_private: (),
}
#[cfg(not(feature = "mlir"))]
impl MlirContext {
pub fn new() -> Self {
Self { _private: () }
}
}
#[cfg(not(feature = "mlir"))]
impl Default for MlirContext {
fn default() -> Self {
Self::new()
}
}
#[cfg(all(test, feature = "mlir"))]
mod tests {
use super::*;
#[test]
fn test_context_creation() {
let _ctx = MlirContext::new();
}
#[test]
fn test_module_creation() {
let ctx = MlirContext::new();
let _module = ctx.create_module("test_module");
}
#[test]
fn test_location_from_span() {
let ctx = MlirContext::new();
let span = Span { start: 10, end: 20 };
let _loc = ctx.location_from_span(&span);
}
#[test]
fn test_default_context() {
let _ctx = MlirContext::default();
}
}