Skip to content

Commit bc1c805

Browse files
authored
fix(cfc): Call statements for function blocks (#1006)
Whenever we encountered a CFC block element, we previously created call statements based on it's typeName. While this is correct for function calls, it is incorrect for function block calls where it should have been the instanceName. To better illustrate the issue, here's the culprit <block localId="4" width="137" height="80" typeName="myFb" instanceName="fb0" executionOrderId="0">. This issue has been fixed by using the instanceName before falling back to the typeName.
1 parent 11ed699 commit bc1c805

File tree

6 files changed

+117
-1
lines changed

6 files changed

+117
-1
lines changed

compiler/plc_xml/src/xml_parser/block.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl<'xml> Block<'xml> {
1616
.collect();
1717

1818
AstFactory::create_call_to(
19-
self.type_name.to_string(),
19+
self.instance_name.as_ref().unwrap_or(&self.type_name).to_string(),
2020
parameters,
2121
session.next_id(),
2222
session.next_id(),

src/resolver.rs

+2
Original file line numberDiff line numberDiff line change
@@ -565,10 +565,12 @@ impl AnnotationMapImpl {
565565

566566
/// annotates the given statement (using it's `get_id()`) with the given type-name
567567
pub fn annotate(&mut self, s: &AstNode, annotation: StatementAnnotation) {
568+
log::trace!("Annotation: {annotation:?} @ {s:?}");
568569
self.type_map.insert(s.get_id(), annotation);
569570
}
570571

571572
pub fn annotate_type_hint(&mut self, s: &AstNode, annotation: StatementAnnotation) {
573+
log::trace!("Annotation (type-hint): {annotation:?} @ {s:?}");
572574
self.type_hint_map.insert(s.get_id(), annotation);
573575
}
574576

tests/integration/cfc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ fn connection_sink_source() {
163163
// THEN the result will have double the value of the initial value
164164
assert_eq!(res, 4);
165165
}
166+
166167
#[test]
167168
fn jump_to_label_with_true() {
168169
let cfc_file = get_test_file("cfc/jump_true.cfc");

tests/integration/cfc/resolver_tests.rs

+26
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,29 @@ fn action_variables_annotated() {
7676
};
7777
assert_debug_snapshot!(annotations.get(left));
7878
}
79+
80+
#[test]
81+
fn function_block_calls_are_annotated_correctly() {
82+
let main = get_test_file("cfc/function_block_call_main.cfc");
83+
let fb = get_test_file("cfc/function_block_call_fb.cfc");
84+
85+
let main = main.load_source(None).unwrap();
86+
let fb = fb.load_source(None).unwrap();
87+
88+
let annotated_project = parse_and_annotate("plc", vec![main, fb]).unwrap();
89+
let annotations = &annotated_project.annotations;
90+
let (unit, ..) = &annotated_project.units[0];
91+
92+
let call_annotation = annotations.get(&unit.implementations[0].statements[0]).unwrap().clone();
93+
assert_debug_snapshot!(call_annotation, @r###"
94+
Variable {
95+
resulting_type: "myFb",
96+
qualified_name: "main.fb0",
97+
constant: false,
98+
argument_type: ByVal(
99+
Local,
100+
),
101+
is_auto_deref: false,
102+
}
103+
"###);
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<pou xmlns="http://www.plcopen.org/xml/tc6_0201" name="myFb" pouType="functionBlock">
3+
<interface>
4+
<localVars/>
5+
<addData>
6+
<data name="www.bachmann.at/plc/plcopenxml" handleUnknown="implementation">
7+
<textDeclaration>
8+
<content>
9+
FUNCTION_BLOCK myFb
10+
VAR_INPUT
11+
in1 : DINT;
12+
in2 : DINT;
13+
END_VAR
14+
15+
VAR_OUTPUT
16+
out1 : DINT;
17+
out2 : DINT;
18+
out3 : DINT;
19+
END_VAR
20+
21+
VAR
22+
23+
END_VAR
24+
</content>
25+
</textDeclaration>
26+
</data>
27+
</addData>
28+
</interface>
29+
<body>
30+
<FBD/>
31+
</body>
32+
</pou>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<pou xmlns="http://www.plcopen.org/xml/tc6_0201" name="main" pouType="program">
3+
<interface>
4+
<localVars/>
5+
<addData>
6+
<data name="www.bachmann.at/plc/plcopenxml" handleUnknown="implementation">
7+
<textDeclaration>
8+
<content>
9+
PROGRAM main
10+
VAR
11+
fb0 : myFb;
12+
END_VAR
13+
</content>
14+
</textDeclaration>
15+
</data>
16+
</addData>
17+
</interface>
18+
<body>
19+
<FBD>
20+
<block localId="4" width="137" height="80" typeName="myFb" instanceName="fb0" executionOrderId="0">
21+
<position x="200" y="110"/>
22+
<inputVariables>
23+
<variable formalParameter="in1" negated="false">
24+
<connectionPointIn>
25+
<relPosition x="0" y="30"/>
26+
</connectionPointIn>
27+
</variable>
28+
<variable formalParameter="in2" negated="false">
29+
<connectionPointIn>
30+
<relPosition x="0" y="50"/>
31+
</connectionPointIn>
32+
</variable>
33+
</inputVariables>
34+
<inOutVariables/>
35+
<outputVariables>
36+
<variable formalParameter="out1" negated="false">
37+
<connectionPointOut>
38+
<relPosition x="137" y="30"/>
39+
</connectionPointOut>
40+
</variable>
41+
<variable formalParameter="out2" negated="false">
42+
<connectionPointOut>
43+
<relPosition x="137" y="50"/>
44+
</connectionPointOut>
45+
</variable>
46+
<variable formalParameter="out3" negated="false">
47+
<connectionPointOut>
48+
<relPosition x="137" y="70"/>
49+
</connectionPointOut>
50+
</variable>
51+
</outputVariables>
52+
</block>
53+
</FBD>
54+
</body>
55+
</pou>

0 commit comments

Comments
 (0)