Skip to content

Commit

Permalink
Make it a function
Browse files Browse the repository at this point in the history
  • Loading branch information
raviqqe committed Dec 6, 2023
1 parent e68c101 commit 3c227e3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 49 deletions.
5 changes: 3 additions & 2 deletions macro/src/dialect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod utility;

use self::{
error::Error,
operation::generate_operation,
utility::{sanitize_documentation, sanitize_snake_case_name},
};
pub use input::DialectInput;
Expand Down Expand Up @@ -64,9 +65,9 @@ fn generate_dialect_module(
.all_derived_definitions("Op")
.map(Operation::new)
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.iter()
.filter(|operation| operation.dialect_name() == dialect_name)
.map(|operation| operation.to_tokens())
.map(|operation| generate_operation(operation))
.collect::<Result<Vec<_>, _>>()?;

let doc = format!(
Expand Down
94 changes: 47 additions & 47 deletions macro/src/dialect/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,63 +334,63 @@ impl<'a> Operation<'a> {
})
.collect()
}
}

pub fn to_tokens(&self) -> Result<TokenStream, Error> {
let class_name = format_ident!("{}", &self.class_name);
let name = &self.full_name;
let accessors = self
.fields()
.map(|field| field.accessors())
.collect::<Result<Vec<_>, _>>()?;
let builder = OperationBuilder::new(self)?;
let builder_tokens = builder.to_tokens()?;
let builder_fn = builder.create_op_builder_fn();
let default_constructor = builder.create_default_constructor()?;
let summary = &self.summary;
let description = &self.description;

Ok(quote! {
#[doc = #summary]
#[doc = "\n\n"]
#[doc = #description]
pub struct #class_name<'c> {
operation: ::melior::ir::operation::Operation<'c>,
}
pub fn generate_operation(operation: &Operation) -> Result<TokenStream, Error> {
let class_name = format_ident!("{}", &operation.class_name);
let name = &operation.full_name;
let accessors = operation
.fields()
.map(|field| field.accessors())
.collect::<Result<Vec<_>, _>>()?;
let builder = OperationBuilder::new(operation)?;
let builder_tokens = builder.to_tokens()?;
let builder_fn = builder.create_op_builder_fn();
let default_constructor = builder.create_default_constructor()?;
let summary = &operation.summary;
let description = &operation.description;

Ok(quote! {
#[doc = #summary]
#[doc = "\n\n"]
#[doc = #description]
pub struct #class_name<'c> {
operation: ::melior::ir::operation::Operation<'c>,
}

impl<'c> #class_name<'c> {
pub fn name() -> &'static str {
#name
}
impl<'c> #class_name<'c> {
pub fn name() -> &'static str {
#name
}

pub fn operation(&self) -> &::melior::ir::operation::Operation<'c> {
&self.operation
}
pub fn operation(&self) -> &::melior::ir::operation::Operation<'c> {
&self.operation
}

#builder_fn
#builder_fn

#(#accessors)*
}
#(#accessors)*
}

#builder_tokens
#builder_tokens

#default_constructor
#default_constructor

impl<'c> TryFrom<::melior::ir::operation::Operation<'c>> for #class_name<'c> {
type Error = ::melior::Error;
impl<'c> TryFrom<::melior::ir::operation::Operation<'c>> for #class_name<'c> {
type Error = ::melior::Error;

fn try_from(
operation: ::melior::ir::operation::Operation<'c>,
) -> Result<Self, Self::Error> {
// TODO Check an operation name.
Ok(Self { operation })
}
fn try_from(
operation: ::melior::ir::operation::Operation<'c>,
) -> Result<Self, Self::Error> {
// TODO Check an operation name.
Ok(Self { operation })
}
}

impl<'c> From<#class_name<'c>> for ::melior::ir::operation::Operation<'c> {
fn from(operation: #class_name<'c>) -> ::melior::ir::operation::Operation<'c> {
operation.operation
}
impl<'c> From<#class_name<'c>> for ::melior::ir::operation::Operation<'c> {
fn from(operation: #class_name<'c>) -> ::melior::ir::operation::Operation<'c> {
operation.operation
}
})
}
}
})
}

0 comments on commit 3c227e3

Please sign in to comment.