From 2a9da447b515f6e06a23d41922f5e07dc6c6ef6f Mon Sep 17 00:00:00 2001 From: Yoshitomo Nakanishi Date: Sat, 7 Sep 2024 16:55:50 +0200 Subject: [PATCH] Implement a macro to provide an easy way to define `Inst` types --- crates/ir/src/inst.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/crates/ir/src/inst.rs b/crates/ir/src/inst.rs index 5deb8739..8be3e220 100644 --- a/crates/ir/src/inst.rs +++ b/crates/ir/src/inst.rs @@ -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 + }; +}