-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add lowering stage to add base-class in new variable-block in pou
- Loading branch information
Showing
10 changed files
with
383 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "plc_lowering" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
plc = { path = "../..", package = "rusty" } | ||
plc_ast = { path = "../plc_ast" } | ||
plc_source = { path = "../plc_source" } | ||
|
||
[dev-dependencies] | ||
insta.workspace = true # TODO: remove individual insta deps from crates | ||
plc_driver = { path = "../plc_driver" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,269 @@ | ||
use plc_ast::{ | ||
ast::{ | ||
CompilationUnit, DataTypeDeclaration, LinkageType, Pou, PouType, Variable, VariableBlock, | ||
VariableBlockType, | ||
}, | ||
mut_visitor::AstVisitorMut, | ||
}; | ||
use plc_source::source_location::SourceLocation; | ||
|
||
pub struct InheritanceLowerer; | ||
|
||
impl InheritanceLowerer { | ||
pub fn visit_unit(&mut self, unit: &mut CompilationUnit) { | ||
self.visit_compilation_unit(unit); | ||
} | ||
} | ||
|
||
impl AstVisitorMut for InheritanceLowerer { | ||
fn visit_pou(&mut self, pou: &mut Pou) { | ||
if !matches!(pou.kind, PouType::FunctionBlock | PouType::Class) { | ||
return; | ||
} | ||
|
||
let Some(base_name) = pou.super_class.as_ref() else { | ||
return; | ||
}; | ||
|
||
let base_var = Variable { | ||
name: "__BASE".to_string(), | ||
data_type_declaration: DataTypeDeclaration::DataTypeReference { | ||
referenced_type: base_name.into(), | ||
location: SourceLocation::internal(), | ||
}, | ||
location: SourceLocation::internal(), | ||
initializer: None, | ||
address: None, | ||
}; | ||
|
||
let block = VariableBlock { | ||
variables: vec![base_var], | ||
variable_block_type: VariableBlockType::Base, | ||
linkage: LinkageType::Internal, | ||
location: SourceLocation::internal(), | ||
..Default::default() | ||
}; | ||
|
||
pou.variable_blocks.insert(0, block); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use insta::assert_debug_snapshot; | ||
use plc_driver::parse_and_annotate; | ||
use plc_source::SourceCode; | ||
|
||
#[test] | ||
fn after_parsing_a_function_block_contains_ref_to_its_base() { | ||
let src: SourceCode = " | ||
FUNCTION_BLOCK foo | ||
END_FUNCTION_BLOCK | ||
FUNCTION_BLOCK bar EXTENDS foo | ||
END_FUNCTION_BLOCK | ||
" | ||
.into(); | ||
|
||
let (_, project) = parse_and_annotate("test", vec![src]).unwrap(); | ||
let unit = &project.units[0].get_unit().units[1]; | ||
assert_debug_snapshot!(unit, @r#" | ||
POU { | ||
name: "bar", | ||
variable_blocks: [ | ||
VariableBlock { | ||
variables: [ | ||
Variable { | ||
name: "__BASE", | ||
data_type: DataTypeReference { | ||
referenced_type: "foo", | ||
}, | ||
}, | ||
], | ||
variable_block_type: Base, | ||
}, | ||
], | ||
pou_type: FunctionBlock, | ||
return_type: None, | ||
interfaces: [], | ||
} | ||
"#); | ||
} | ||
|
||
#[test] | ||
fn write_to_parent_variable_qualified_access() { | ||
let src: SourceCode = " | ||
FUNCTION_BLOCK fb | ||
VAR | ||
x : INT; | ||
y : INT; | ||
END_VAR | ||
END_FUNCTION_BLOCK | ||
FUNCTION_BLOCK fb2 EXTENDS fb | ||
END_FUNCTION_BLOCK | ||
FUNCTION_BLOCK foo | ||
VAR | ||
myFb : fb2; | ||
END_VAR | ||
myFb.x := 1; | ||
END_FUNCTION_BLOCK | ||
" | ||
.into(); | ||
|
||
let (_, project) = parse_and_annotate("test", vec![src]).unwrap(); | ||
let unit = &project.units[0].get_unit().implementations[2]; | ||
assert_debug_snapshot!(unit, @r#" | ||
Implementation { | ||
name: "foo", | ||
type_name: "foo", | ||
linkage: Internal, | ||
pou_type: FunctionBlock, | ||
statements: [ | ||
Assignment { | ||
left: ReferenceExpr { | ||
kind: Member( | ||
Identifier { | ||
name: "x", | ||
}, | ||
), | ||
base: Some( | ||
ReferenceExpr { | ||
kind: Member( | ||
Identifier { | ||
name: "myFb", | ||
}, | ||
), | ||
base: Some( | ||
ReferenceExpr { | ||
kind: Member( | ||
Identifier { | ||
name: "__BASE", | ||
}, | ||
), | ||
base: None, | ||
}, | ||
), | ||
}, | ||
), | ||
}, | ||
right: LiteralInteger { | ||
value: 1, | ||
}, | ||
}, | ||
], | ||
location: SourceLocation { | ||
span: Range( | ||
TextLocation { | ||
line: 15, | ||
column: 16, | ||
offset: 347, | ||
}..TextLocation { | ||
line: 15, | ||
column: 28, | ||
offset: 359, | ||
}, | ||
), | ||
}, | ||
name_location: SourceLocation { | ||
span: Range( | ||
TextLocation { | ||
line: 11, | ||
column: 27, | ||
offset: 262, | ||
}..TextLocation { | ||
line: 11, | ||
column: 30, | ||
offset: 265, | ||
}, | ||
), | ||
}, | ||
overriding: false, | ||
generic: false, | ||
access: None, | ||
} | ||
"#); | ||
} | ||
|
||
#[test] | ||
fn write_to_parent_variable_in_instance() { | ||
let src: SourceCode = r#" | ||
FUNCTION_BLOCK foo | ||
VAR | ||
s : STRING; | ||
END_VAR | ||
END_FUNCTION_BLOCK | ||
FUNCTION_BLOCK bar EXTENDS foo | ||
s := 'world'; | ||
END_FUNCTION_BLOCK | ||
"# | ||
.into(); | ||
|
||
let (_, project) = parse_and_annotate("test", vec![src]).unwrap(); | ||
let unit = &project.units[0].get_unit().implementations[1]; | ||
assert_debug_snapshot!(unit, @r#" | ||
Implementation { | ||
name: "bar", | ||
type_name: "bar", | ||
linkage: Internal, | ||
pou_type: FunctionBlock, | ||
statements: [ | ||
Assignment { | ||
left: ReferenceExpr { | ||
kind: Member( | ||
Identifier { | ||
name: "s", | ||
}, | ||
), | ||
base: Some( | ||
ReferenceExpr { | ||
kind: Member( | ||
Identifier { | ||
name: "__BASE", | ||
}, | ||
), | ||
base: None, | ||
}, | ||
), | ||
}, | ||
right: LiteralString { | ||
value: "world", | ||
is_wide: false, | ||
}, | ||
}, | ||
], | ||
location: SourceLocation { | ||
span: Range( | ||
TextLocation { | ||
line: 8, | ||
column: 16, | ||
offset: 189, | ||
}..TextLocation { | ||
line: 8, | ||
column: 29, | ||
offset: 202, | ||
}, | ||
), | ||
}, | ||
name_location: SourceLocation { | ||
span: Range( | ||
TextLocation { | ||
line: 7, | ||
column: 27, | ||
offset: 156, | ||
}..TextLocation { | ||
line: 7, | ||
column: 30, | ||
offset: 159, | ||
}, | ||
), | ||
}, | ||
overriding: false, | ||
generic: false, | ||
access: None, | ||
} | ||
"#); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// pub use plc::lowering::*; | ||
pub mod inheritance; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.