From 119ea09646be6f12c79935dbdfebef1aea9ae435 Mon Sep 17 00:00:00 2001 From: Michael Haselberger Date: Wed, 19 Jun 2024 16:29:58 +0200 Subject: [PATCH] extends macro to handle multiple borrows on `AstNode` parameter --- Cargo.lock | 4 ++-- compiler/plc_ast/src/ast.rs | 4 ++++ compiler/plc_ast/src/pre_processor.rs | 20 +++++++++----------- compiler/plc_xml/src/xml_parser/fbd.rs | 11 +++++------ 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0cceff0fa2..7d7eeb65ba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1446,7 +1446,7 @@ dependencies = [ "httpdate", "itoa", "pin-project-lite", - "socket2 0.5.6", + "socket2 0.5.7", "tokio", "tower-service", "tracing", @@ -2908,7 +2908,7 @@ dependencies = [ name = "rustc-demangle" version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" [[package]] name = "rustc-hash" diff --git a/compiler/plc_ast/src/ast.rs b/compiler/plc_ast/src/ast.rs index 28e5c53fb0..66cf5fe9b9 100644 --- a/compiler/plc_ast/src/ast.rs +++ b/compiler/plc_ast/src/ast.rs @@ -638,9 +638,13 @@ pub enum AstStatement { /// Will try to return a reference to the variants inner type, specified via the `t:ty` parameter. /// Converts the `try_from`-`Result` into an `Option` macro_rules! try_from { + () => { None }; ($ex:expr, $t:ty) => { <&$t>::try_from($ex.get_stmt()).ok() }; + ($($ex:tt)*, $t:ty) => { + try_from!($($ex)*, $t).ok() + }; } impl Debug for AstNode { diff --git a/compiler/plc_ast/src/pre_processor.rs b/compiler/plc_ast/src/pre_processor.rs index 049cbd2a69..de4d0d7247 100644 --- a/compiler/plc_ast/src/pre_processor.rs +++ b/compiler/plc_ast/src/pre_processor.rs @@ -10,7 +10,7 @@ use crate::{ DataTypeDeclaration, Operator, Pou, UserTypeDeclaration, Variable, }, literals::AstLiteral, - provider::IdProvider, + provider::IdProvider, try_from, }; use plc_source::source_location::SourceLocation; @@ -96,17 +96,15 @@ pub fn pre_process(unit: &mut CompilationUnit, mut id_provider: IdProvider) { let initialized_enum_elements = flatten_expression_list(original_elements) .iter() - .map(|it| match &it.stmt { - AstStatement::Assignment(Assignment { left, right }) => { - // - ( - extract_flat_ref_name(left.as_ref()), - Some(*right.clone()), - it.get_location(), + .map(|it| try_from!(it, Assignment).map_or_else( + || (extract_flat_ref_name(it), None, it.get_location()), + | Assignment { left, right } | ( + extract_flat_ref_name(left.as_ref()), + Some(*right.clone()), + it.get_location(), ) - } - _ => (extract_flat_ref_name(it), None, it.get_location()), - }) + ) + ) .map(|(element_name, initializer, location)| { let enum_literal = initializer.unwrap_or_else(|| { build_enum_initializer(&last_name, &location, &mut id_provider, enum_name) diff --git a/compiler/plc_xml/src/xml_parser/fbd.rs b/compiler/plc_xml/src/xml_parser/fbd.rs index a87cdb420a..ada03b03f3 100644 --- a/compiler/plc_xml/src/xml_parser/fbd.rs +++ b/compiler/plc_xml/src/xml_parser/fbd.rs @@ -1,4 +1,4 @@ -use ast::ast::{AstFactory, AstNode, AstStatement}; +use ast::{ast::{AstFactory, AstNode, CallStatement}, try_from}; use plc::index::FxIndexMap; use plc_source::source_location::SourceLocation; @@ -54,11 +54,10 @@ impl<'xml> FunctionBlockDiagram<'xml> { let (rhs, remove_id) = ast_association .get(&ref_id) .map(|stmt| { - if matches!(stmt.get_stmt(), AstStatement::CallStatement(..)) { - (stmt.clone(), Some(ref_id)) - } else { - self.transform_node(ref_id, session, ast_association) - } + try_from!(stmt, CallStatement).map_or_else( + || self.transform_node(ref_id, session, ast_association), + |_| (stmt.clone(), Some(ref_id)) + ) }) .expect("Expected AST statement, found None");