Skip to content

Commit c186d0e

Browse files
authored
refactor(AST): introduce AstVisitor trait (#1231)
* refactor(AST): introduce AstVisitor trait The AST-Visitor trait allows generic visiting of AST-nodes. A default Walking behavior is implemented for each AstStatement but it can be altered by any implementor. When overriding a visit_XXX method, the implementation can decide to continue with the default walking behavior (by calling the walk function on the passed AstStatement-Struct, to skip it, or to continue with an alternative walking bahavior. removed unused AST element CastStatement
1 parent 450566c commit c186d0e

File tree

9 files changed

+1346
-85
lines changed

9 files changed

+1346
-85
lines changed

compiler/plc_ast/src/ast.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,6 @@ pub enum AstStatement {
600600
DefaultValue(DefaultValue),
601601
// Literals
602602
Literal(AstLiteral),
603-
CastStatement(CastStatement),
604603
MultipliedStatement(MultipliedStatement),
605604
// Expressions
606605
ReferenceExpr(ReferenceExpr),
@@ -735,9 +734,6 @@ impl Debug for AstNode {
735734
}
736735
AstStatement::ContinueStatement(..) => f.debug_struct("ContinueStatement").finish(),
737736
AstStatement::ExitStatement(..) => f.debug_struct("ExitStatement").finish(),
738-
AstStatement::CastStatement(CastStatement { target, type_name }) => {
739-
f.debug_struct("CastStatement").field("type_name", type_name).field("target", target).finish()
740-
}
741737
AstStatement::ReferenceExpr(ReferenceExpr { access, base }) => {
742738
f.debug_struct("ReferenceExpr").field("kind", access).field("base", base).finish()
743739
}

compiler/plc_ast/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ pub mod control_statements;
77
pub mod literals;
88
mod pre_processor;
99
pub mod provider;
10+
pub mod visitor;

compiler/plc_ast/src/visitor.rs

Lines changed: 714 additions & 0 deletions
Large diffs are not rendered by default.

src/builtins.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -846,20 +846,6 @@ fn generate_variable_length_array_bound_function<'ink>(
846846
let offset = if is_lower { (value - 1) as u64 * 2 } else { (value - 1) as u64 * 2 + 1 };
847847
llvm.i32_type().const_int(offset, false)
848848
}
849-
AstStatement::CastStatement(data) => {
850-
let ExpressionValue::RValue(value) = generator.generate_expression_value(&data.target)? else {
851-
unreachable!()
852-
};
853-
854-
if !value.is_int_value() {
855-
return Err(Diagnostic::codegen_error(
856-
format!("Expected INT value, found {}", value.get_type()),
857-
location,
858-
));
859-
};
860-
861-
value.into_int_value()
862-
}
863849
// e.g. LOWER_BOUND(arr, idx + 3)
864850
_ => {
865851
let expression_value = generator.generate_expression(params[1])?;

src/codegen/generators/expression_generator.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1751,7 +1751,6 @@ impl<'ink, 'b> ExpressionCodeGenerator<'ink, 'b> {
17511751
}
17521752
// if there is just one assignment, this may be an struct-initialization (TODO this is not very elegant :-/ )
17531753
AstStatement::Assignment { .. } => self.generate_literal_struct(literal_statement),
1754-
AstStatement::CastStatement(data) => self.generate_expression_value(&data.target),
17551754
_ => Err(cannot_generate_literal()),
17561755
}
17571756
}

src/parser/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use plc_ast::{
55
use plc_source::source_location::SourceLocation;
66

77
// Copyright (c) 2020 Ghaith Hachem and Mathias Rieder
8+
mod ast_visitor_tests;
89
mod class_parser_tests;
910
mod container_parser_tests;
1011
mod control_parser_tests;

0 commit comments

Comments
 (0)