mlirformat-macros 0.2.0

Proc macros for mlirformat: #[mlir::op], #[mlir::dialect]
Documentation
//! `#[mlir::op]` and `#[mlir::dialect]`. Neither writes conversion code: `op` spells each
//! field's MLIR role in its serde name (decoded by `mlirformat::Field`) and forwards the
//! conversions to the runtime; `dialect` turns an enum of ops into a registrable op table.
//!
//! ```ignore
//! #[mlir::op("arith.maxsi", format = "$lhs `,` $rhs attr-dict `:` type($result)", results(result))]
//! pub struct ArithMax<'a> {
//!     #[slot(result)] pub lhs: Value<'a>,     // operand; its type is type variable `result`
//!     #[slot(result)] pub rhs: Value<'a>,
//!     #[dtype] pub result: Type<'a>,          // type variable (result #0 by `results(...)`)
//! }
//! ```
//!
//! Field markers (a `Vec` field is variadic; MLIR names are the lowerCamelCase field names
//! unless `name = "..."`):
//!
//! ```text
//! #[slot(T)]                       operand(s) typed by type variable T
//! #[dtype] #[dtype(i1)] #[dtype(i1 = x)] #[dtype(of = attr)] #[dtype(eq = x)] #[dtype(is = index)] #[dtype(el = x)]
//!                                  type variable; how custom form infers it when unspelled
//! #[attr] #[attr(int = "i32")] #[attr(dialect = "arith.overflow", default = "<none>")]
//!                                  property (`bool`: unit attribute; enum: case value typed
//!                                  `int`, default i64); a dialect attribute is
//!                                  spelled without its prefix, its default is omitted
//! #[region]  #[successor]
//! ```
//!
//! `attrs` (discardable attributes) and `loc` fields are added. Options: `format = "..."`,
//! `results(a, b)`, `custom` (no `Op` impl: `print` / `parse` written by hand), `generic` (no custom
//! form: always printed generic).
//!
//! `#[mlir::dialect("ns", params = f)]`: `f` structures `!ns.x<...>` / `#ns.x<...>` bodies
//! (`Dialect::parse_params`).

use proc_macro::TokenStream;
use syn::{Error, ItemEnum, ItemStruct, parse_macro_input};

mod dialect;
mod op;

#[proc_macro_attribute]
pub fn op(args: TokenStream, item: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as op::OpArgs);
    let item = parse_macro_input!(item as ItemStruct);
    args.expand(item).unwrap_or_else(Error::into_compile_error).into()
}

#[proc_macro_attribute]
pub fn dialect(args: TokenStream, item: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as dialect::DialectArgs);
    let item = parse_macro_input!(item as ItemEnum);
    dialect::expand(args, item).unwrap_or_else(Error::into_compile_error).into()
}