From 5c841c392d1478409d97eab7ddbf12c281fcf67d Mon Sep 17 00:00:00 2001 From: TheCPP Date: Fri, 23 Aug 2024 15:23:41 +0200 Subject: [PATCH] [TYPE] using more safe if let Some(...) instead of .unwrap() --- src/IR/ir.rs | 57 ++++------------------------ src/Optimizations/Passes/MathEval.rs | 54 ++++++++++++-------------- 2 files changed, 31 insertions(+), 80 deletions(-) diff --git a/src/IR/ir.rs b/src/IR/ir.rs index 21ef428f..1252bc8f 100644 --- a/src/IR/ir.rs +++ b/src/IR/ir.rs @@ -94,7 +94,7 @@ IrTypeWith3!(And, T, U, Z); use crate::Support::{ColorClass, ColorProfile}; macro_rules! MathIrNode { - ($name:ident, $compileFuncVarVar:ident, $compileFuncVarTy:ident, $compileFuncTyTy:ident, $buildTraitName:ident, $buildFuncName:ident, $dump:expr, $variantVarVar:expr, $variantVarType:expr, $variantTypeType:expr) => { + ($name:ident, $compileFuncVarVar:ident, $compileFuncVarTy:ident, $compileFuncTyTy:ident, $buildTraitName:ident, $buildFuncName:ident, $dump:expr) => { /// Used for overloading the build function pub trait $buildTraitName { /// Xors values @@ -151,10 +151,6 @@ macro_rules! MathIrNode { Box::new(self.clone()) } - fn name(&self) -> String { - $variantTypeType.into() - } - fn dump(&self) -> String { format!("{} = {} {} {}, {}", $dump, self.inner3.name, self.inner3.ty, self.inner1.val(), self.inner2.val()) } @@ -205,11 +201,7 @@ macro_rules! MathIrNode { fn clone_box(&self) -> Box { Box::new(self.clone()) } - - fn name(&self) -> String { - $variantVarVar.into() - } - + fn dump(&self) -> String { format!("{} = {} {} {}, {}", $dump, self.inner3.name, self.inner3.ty, self.inner1.name, self.inner2.name) } @@ -261,10 +253,6 @@ macro_rules! MathIrNode { Box::new(self.clone()) } - fn name(&self) -> String { - $variantVarType.into() - } - fn dump(&self) -> String { format!("{} = {} {} {}, {}", $dump, self.inner3.name, self.inner1.ty, self.inner1.name, self.inner2.val()) } @@ -314,11 +302,11 @@ macro_rules! MathIrNode { }; } -MathIrNode!(Add, getCompileFuncForAddVarVar, getCompileFuncForAddVarType, getCompileFuncForAddTypeType, BuildAdd, BuildAdd, "add", "AddVarVar", "AddVarType", "AddTypeType"); -MathIrNode!(Sub, getCompileFuncForSubVarVar, getCompileFuncForSubVarType, getCompileFuncForSubTypeType, BuildSub, BuildSub, "sub", "SubVarVar", "SubVarType", "SubTypeType"); -MathIrNode!(Xor, getCompileFuncForXorVarVar, getCompileFuncForXorVarType, getCompileFuncForXorTypeType, BuildXor, BuildXor, "xor", "XorVarVar", "XorVarType", "XorTypeType"); -MathIrNode!(Or, getCompileFuncForOrVarVar, getCompileFuncForOrVarType, getCompileFuncForOrTypeType, BuildOr, BuildOr, "or", "OrVarVar", "OrVarType", "OrTypeType"); -MathIrNode!(And, getCompileFuncForAndVarVar, getCompileFuncForAndVarType, getCompileFuncForAndTypeType, BuildAnd, BuildAnd, "and", "AndVarVar", "AndVarType", "AndTypeType"); +MathIrNode!(Add, getCompileFuncForAddVarVar, getCompileFuncForAddVarType, getCompileFuncForAddTypeType, BuildAdd, BuildAdd, "add"); +MathIrNode!(Sub, getCompileFuncForSubVarVar, getCompileFuncForSubVarType, getCompileFuncForSubTypeType, BuildSub, BuildSub, "sub"); +MathIrNode!(Xor, getCompileFuncForXorVarVar, getCompileFuncForXorVarType, getCompileFuncForXorTypeType, BuildXor, BuildXor, "xor"); +MathIrNode!(Or, getCompileFuncForOrVarVar, getCompileFuncForOrVarType, getCompileFuncForOrTypeType, BuildOr, BuildOr, "or"); +MathIrNode!(And, getCompileFuncForAndVarVar, getCompileFuncForAndVarType, getCompileFuncForAndTypeType, BuildAnd, BuildAnd, "and"); impl Ir for Return { @@ -326,10 +314,6 @@ impl Ir for Return { Box::new(self.clone()) } - fn name(&self) -> String { - "RetType".into() - } - fn dump(&self) -> String { let metadata: TypeMetadata = self.inner1.into(); format!("ret {} {}", metadata, self.inner1.val()) @@ -368,10 +352,6 @@ impl Ir for Return { Box::new(self.clone()) } - fn name(&self) -> String { - "RetVar".into() - } - fn dump(&self) -> String { format!("ret {} {}", self.inner1.ty, self.inner1.name) } @@ -423,10 +403,6 @@ impl Ir for ConstAssign { ) } - fn name(&self) -> String { - "AssignVarType".into() - } - fn as_any(&self) -> &dyn Any { self } @@ -470,10 +446,6 @@ impl Ir for ConstAssign { ) } - fn name(&self) -> String { - "AssignVarVar".into() - } - fn as_any(&self) -> &dyn Any { self } @@ -516,10 +488,6 @@ impl Ir for ConstAssign { ) } - fn name(&self) -> String { - "AssignVarConst".into() - } - fn as_any(&self) -> &dyn Any { self } @@ -557,10 +525,6 @@ impl Ir for Cast { ) } - fn name(&self) -> String { - "CastTyVar".into() - } - fn as_any(&self) -> &dyn Any { self } @@ -617,10 +581,6 @@ impl Ir for Call, Var> { ) } - fn name(&self) -> String { - "Call".to_string() - } - fn as_any(&self) -> &dyn Any { self } @@ -781,9 +741,6 @@ pub(crate) trait Ir: Debug + Any { /// Returns the ir node as his textual representation with colors fn dumpColored(&self, profile: ColorProfile) -> String; - /// Returns the name of the ir expr - fn name(&self) -> String; - /// Turns the ir node to an any fn as_any(&self) -> &dyn Any; diff --git a/src/Optimizations/Passes/MathEval.rs b/src/Optimizations/Passes/MathEval.rs index d82d5f53..967ac976 100644 --- a/src/Optimizations/Passes/MathEval.rs +++ b/src/Optimizations/Passes/MathEval.rs @@ -12,12 +12,10 @@ pub fn PreComputeValue() -> Box { impl Pass for PreComputeValue { fn run(&self, block: &mut crate::prelude::Block) { for node in block.nodes.iter_mut() { - if &node.name() == "AddTypeType" { - let any = node.as_any(); - let add_node = any.downcast_ref::>().unwrap().clone(); - let res = add_node.inner1.val() + add_node.inner2.val(); - node.replace(ConstAssign::new(add_node.inner3.clone(), { - match add_node.inner3.ty { + if let Some(or) = node.as_any().downcast_ref::>() { + let res = or.inner1.val() + or.inner2.val(); + node.replace(ConstAssign::new(or.inner3.clone(), { + match or.inner3.ty { TypeMetadata::u16 => Type::u16(res as u16), TypeMetadata::u32 => Type::u32(res as u32), TypeMetadata::u64 => Type::u64(res as u64), @@ -28,12 +26,11 @@ impl Pass for PreComputeValue { TypeMetadata::Void => Type::Void, } })) - } else if &node.name() == "SubTypeType" { - let any = node.as_any(); - let sub_node = any.downcast_ref::>().unwrap().clone(); - let res = sub_node.inner1.val() - sub_node.inner2.val(); - node.replace(ConstAssign::new(sub_node.inner3.clone(), { - match sub_node.inner3.ty { + } + if let Some(or) = node.as_any().downcast_ref::>() { + let res = or.inner1.val() - or.inner2.val(); + node.replace(ConstAssign::new(or.inner3.clone(), { + match or.inner3.ty { TypeMetadata::u16 => Type::u16(res as u16), TypeMetadata::u32 => Type::u32(res as u32), TypeMetadata::u64 => Type::u64(res as u64), @@ -44,12 +41,11 @@ impl Pass for PreComputeValue { TypeMetadata::Void => Type::Void, } })) - } else if &node.name() == "XorTypeType" { - let any = node.as_any(); - let xor_node = any.downcast_ref::>().unwrap().clone(); - let res = xor_node.inner1.val() ^ xor_node.inner2.val(); - node.replace(ConstAssign::new(xor_node.inner3.clone(), { - match xor_node.inner3.ty { + } + if let Some(or) = node.as_any().downcast_ref::>() { + let res = or.inner1.val() & or.inner2.val(); + node.replace(ConstAssign::new(or.inner3.clone(), { + match or.inner3.ty { TypeMetadata::u16 => Type::u16(res as u16), TypeMetadata::u32 => Type::u32(res as u32), TypeMetadata::u64 => Type::u64(res as u64), @@ -60,12 +56,11 @@ impl Pass for PreComputeValue { TypeMetadata::Void => Type::Void, } })) - } else if &node.name() == "AndTypeType" { - let any = node.as_any(); - let and_node = any.downcast_ref::>().unwrap().clone(); - let res = and_node.inner1.val() & and_node.inner2.val(); - node.replace(ConstAssign::new(and_node.inner3.clone(), { - match and_node.inner3.ty { + } + if let Some(or) = node.as_any().downcast_ref::>() { + let res = or.inner1.val() ^ or.inner2.val(); + node.replace(ConstAssign::new(or.inner3.clone(), { + match or.inner3.ty { TypeMetadata::u16 => Type::u16(res as u16), TypeMetadata::u32 => Type::u32(res as u32), TypeMetadata::u64 => Type::u64(res as u64), @@ -76,12 +71,11 @@ impl Pass for PreComputeValue { TypeMetadata::Void => Type::Void, } })) - } else if &node.name() == "OrTypeType" { - let any = node.as_any(); - let or_node = any.downcast_ref::>().unwrap().clone(); - let res = or_node.inner1.val() | or_node.inner2.val(); - node.replace(ConstAssign::new(or_node.inner3.clone(), { - match or_node.inner3.ty { + } + if let Some(or) = node.as_any().downcast_ref::>() { + let res = or.inner1.val() | or.inner2.val(); + node.replace(ConstAssign::new(or.inner3.clone(), { + match or.inner3.ty { TypeMetadata::u16 => Type::u16(res as u16), TypeMetadata::u32 => Type::u32(res as u32), TypeMetadata::u64 => Type::u64(res as u64),