Skip to content

Commit

Permalink
Implement a macro to provide an easy way to define Inst types
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Nak committed Sep 7, 2024
1 parent dd3048f commit 2a9da44
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions crates/ir/src/inst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,43 @@ pub trait Inst {
fn has_side_effect(&self) -> bool;
fn as_str(&self) -> &'static str;
}

macro_rules! define_inst {
($(($purity:ident, $ty:ident, $name:literal, $arity:literal)),* $(,)?) => {
$(
define_inst!($purity, $ty, $name, $arity);
)*
};

($purity: ident, $ty: ident, $name:literal, $arity:literal) => {
pub struct $ty {
args: [Value; $arity],
}

impl Inst for $ty {
fn args(&self) -> &[Value] {
&self.args
}

fn args_mut(&mut self) -> &mut [Value] {
&mut self.args
}

fn has_side_effect(&self) -> bool {
define_inst!(has_side_effect_impl $purity)
}

fn as_str(&self) -> &'static str {
$name
}
}
};

(has_side_effect_impl pure) => {
true
};

(has_side_effect_impl non_pure) => {
true
};
}

0 comments on commit 2a9da44

Please sign in to comment.