refactor(ast_tools): simplify define_pass! macro (#6901)

This commit is contained in:
overlookmotel 2024-10-25 21:04:03 +00:00
parent 1d981bfa53
commit 26de0de326
3 changed files with 24 additions and 17 deletions

View file

@ -21,9 +21,9 @@ compile_error!("This module only supports 64bit architectures.");
type WellKnown = FxHashMap<&'static str, PlatformLayout>;
define_pass! {
pub struct CalcLayout;
}
pub struct CalcLayout;
define_pass!(CalcLayout);
impl Pass for CalcLayout {
fn each(&mut self, ty: &mut AstType, ctx: &EarlyCtx) -> crate::Result<bool> {

View file

@ -27,9 +27,9 @@ impl Unresolved for Vec<Inherit> {
}
}
define_pass! {
pub struct Linker;
}
pub struct Linker;
define_pass!(Linker);
impl Pass for Linker {
/// # Panics

View file

@ -43,19 +43,26 @@ pub trait Pass {
}
macro_rules! define_pass {
($vis:vis struct $ident:ident $($lifetime:lifetime)? $($rest:tt)*) => {
$vis struct $ident $($lifetime)? $($rest)*
impl $($lifetime)? $crate::codegen::Runner for $ident $($lifetime)? {
type Context = $crate::codegen::EarlyCtx;
type Output = ();
fn name(&self) -> &'static str {
stringify!($ident)
}
($ident:ident $($lifetime:lifetime)?) => {
const _: () = {
use $crate::{
codegen::{EarlyCtx, Runner},
Result,
};
fn run(&mut self, ctx: &Self::Context) -> $crate::Result<Self::Output> {
self.call(ctx).map(|_| ())
impl $($lifetime)? Runner for $ident $($lifetime)? {
type Context = EarlyCtx;
type Output = ();
fn name(&self) -> &'static str {
stringify!($ident)
}
fn run(&mut self, ctx: &Self::Context) -> Result<()> {
self.call(ctx).map(|_| ())
}
}
}
};
};
}