Skip to content

Commit 25e79cc

Browse files
authored
Merge branch 'master' into abroooo/fix_dbg_alignment
2 parents 3ab7b23 + 7f35d82 commit 25e79cc

37 files changed

+2137
-600
lines changed

compiler/plc_ast/src/ast.rs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use std::{
44
fmt::{Debug, Display, Formatter},
5+
hash::Hash,
56
ops::Range,
67
};
78

@@ -54,10 +55,11 @@ pub struct Pou {
5455
#[derive(Debug, PartialEq)]
5556
pub struct Interface {
5657
pub id: AstId,
57-
pub identifier: Identifier,
58-
pub methods: Vec<Pou>,
58+
pub ident: Identifier,
5959
pub location: SourceLocation,
60+
pub methods: Vec<Pou>,
6061
pub extensions: Vec<Identifier>,
62+
pub properties: Vec<PropertyBlock>,
6163
}
6264

6365
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
@@ -69,12 +71,21 @@ pub struct Identifier {
6971
/// The property container as a whole, which contains [`PropertyImplementation`]s
7072
#[derive(Debug, PartialEq, Clone)]
7173
pub struct PropertyBlock {
72-
pub name: Identifier,
73-
pub return_type: DataTypeDeclaration,
74+
pub ident: Identifier,
75+
pub datatype: DataTypeDeclaration,
7476
pub implementations: Vec<PropertyImplementation>,
7577
}
7678

77-
/// The declaration and implementation of a properties accessor (GET or SET)
79+
impl Eq for PropertyBlock {}
80+
81+
impl Hash for PropertyBlock {
82+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
83+
self.ident.hash(state);
84+
self.datatype.get_name().hash(state);
85+
self.datatype.get_location().hash(state);
86+
}
87+
}
88+
7889
#[derive(Debug, PartialEq, Clone)]
7990
pub struct PropertyImplementation {
8091
pub kind: PropertyKind,
@@ -84,24 +95,12 @@ pub struct PropertyImplementation {
8495
pub end_location: SourceLocation,
8596
}
8697

87-
#[derive(Debug, PartialEq, Clone, Copy)]
98+
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
8899
pub enum PropertyKind {
89100
Get,
90101
Set,
91102
}
92103

93-
impl From<&PropertyBlock> for Variable {
94-
fn from(value: &PropertyBlock) -> Self {
95-
Variable {
96-
name: value.name.name.clone(),
97-
data_type_declaration: value.return_type.clone(),
98-
initializer: None,
99-
address: None,
100-
location: value.name.location.clone(),
101-
}
102-
}
103-
}
104-
105104
impl std::fmt::Display for PropertyKind {
106105
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
107106
match self {
@@ -353,6 +352,9 @@ pub enum PouType {
353352
/// The parent of this method, i.e. a function block, class or an interface
354353
parent: String,
355354

355+
/// The property name (pre-mangled) and its type, if the method originated from a property
356+
property: Option<(String, PropertyKind)>,
357+
356358
declaration_kind: DeclarationKind,
357359
},
358360
Init,
@@ -494,14 +496,14 @@ pub struct VariableBlock {
494496
pub constant: bool,
495497
pub retain: bool,
496498
pub variables: Vec<Variable>,
497-
pub variable_block_type: VariableBlockType,
499+
pub kind: VariableBlockType,
498500
pub linkage: LinkageType,
499501
pub location: SourceLocation,
500502
}
501503

502504
impl VariableBlock {
503505
pub fn with_block_type(mut self, block_type: VariableBlockType) -> Self {
504-
self.variable_block_type = block_type;
506+
self.kind = block_type;
505507
self
506508
}
507509

@@ -518,7 +520,7 @@ impl Default for VariableBlock {
518520
constant: false,
519521
retain: false,
520522
variables: vec![],
521-
variable_block_type: VariableBlockType::Local,
523+
kind: VariableBlockType::Local,
522524
linkage: LinkageType::Internal,
523525
location: SourceLocation::internal(),
524526
}
@@ -529,7 +531,7 @@ impl Debug for VariableBlock {
529531
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
530532
f.debug_struct("VariableBlock")
531533
.field("variables", &self.variables)
532-
.field("variable_block_type", &self.variable_block_type)
534+
.field("variable_block_type", &self.kind)
533535
.finish()
534536
}
535537
}
@@ -1379,8 +1381,12 @@ mod tests {
13791381
assert_eq!(PouType::Action.to_string(), "Action");
13801382
assert_eq!(PouType::Class.to_string(), "Class");
13811383
assert_eq!(
1382-
PouType::Method { parent: String::new(), declaration_kind: DeclarationKind::Concrete }
1383-
.to_string(),
1384+
PouType::Method {
1385+
parent: String::new(),
1386+
property: None,
1387+
declaration_kind: DeclarationKind::Concrete
1388+
}
1389+
.to_string(),
13841390
"Method"
13851391
);
13861392
}

compiler/plc_ast/src/pre_processor.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,11 @@ fn process_var_config_variables(unit: &mut CompilationUnit) {
224224
}
225225

226226
fn update_generated_globals(unit: &mut CompilationUnit, mangled_globals: Vec<Variable>) {
227-
let mut block = if let Some(index) = unit.global_vars.iter().position(|block| {
228-
block.variable_block_type == VariableBlockType::Global && block.location.is_builtin_internal()
229-
}) {
227+
let mut block = if let Some(index) = unit
228+
.global_vars
229+
.iter()
230+
.position(|block| block.kind == VariableBlockType::Global && block.location.is_builtin_internal())
231+
{
230232
unit.global_vars.remove(index)
231233
} else {
232234
VariableBlock::default().with_block_type(VariableBlockType::Global)
@@ -243,9 +245,7 @@ fn update_generated_globals(unit: &mut CompilationUnit, mangled_globals: Vec<Var
243245
fn get_internal_global_block(unit: &CompilationUnit) -> Option<&VariableBlock> {
244246
unit.global_vars
245247
.iter()
246-
.position(|block| {
247-
block.variable_block_type == VariableBlockType::Global && block.location.is_builtin_internal()
248-
})
248+
.position(|block| block.kind == VariableBlockType::Global && block.location.is_builtin_internal())
249249
.and_then(|index| unit.global_vars.get(index))
250250
}
251251

compiler/plc_driver/src/pipelines.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use plc::{
2626
linker::LinkerType,
2727
lowering::{
2828
property::PropertyLowerer,
29-
validator::ParticipantValidator,
3029
{calls::AggregateTypeLowerer, InitVisitor},
3130
},
3231
output::FormatOption,
@@ -57,7 +56,6 @@ use toml;
5756

5857
pub mod participant;
5958
pub mod property;
60-
pub mod validator;
6159

6260
pub struct BuildPipeline<T: SourceContainer> {
6361
pub context: GlobalContext,
@@ -258,22 +256,15 @@ impl<T: SourceContainer> BuildPipeline<T> {
258256
/// Register all default participants (excluding codegen/linking)
259257
pub fn register_default_participants(&mut self) {
260258
use participant::InitParticipant;
261-
// XXX: should we use a static array of participants?
262-
let participants: Vec<Box<dyn PipelineParticipant>> = vec![Box::new(ParticipantValidator::new(
263-
&self.context,
264-
self.compile_parameters.as_ref().map(|it| it.error_format).unwrap_or_default(),
265-
))];
266259

260+
// XXX: should we use a static array of participants?
267261
let mut_participants: Vec<Box<dyn PipelineParticipantMut>> = vec![
268262
Box::new(PropertyLowerer::new(self.context.provider())),
269263
Box::new(InitParticipant::new(self.project.get_init_symbol_name(), self.context.provider())),
270264
Box::new(AggregateTypeLowerer::new(self.context.provider())),
271265
Box::new(InheritanceLowerer::new(self.context.provider())),
272266
];
273267

274-
for participant in participants {
275-
self.register_participant(participant)
276-
}
277268
for participant in mut_participants {
278269
self.register_mut_participant(participant)
279270
}

compiler/plc_driver/src/pipelines/property.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ impl PipelineParticipantMut for PropertyLowerer {
1111
let ParsedProject { mut units } = parsed_project;
1212

1313
for unit in &mut units {
14-
self.lower_properties_to_pous(unit);
14+
self.properties_to_pous(unit);
1515
}
1616

1717
ParsedProject { units }
@@ -22,7 +22,7 @@ impl PipelineParticipantMut for PropertyLowerer {
2222
self.annotations = Some(annotations);
2323

2424
for AnnotatedUnit { unit, .. } in &mut units.iter_mut() {
25-
self.lower_references_to_calls(unit);
25+
self.properties_to_fncalls(unit);
2626
}
2727

2828
let project = IndexedProject {

compiler/plc_driver/src/pipelines/validator.rs

Lines changed: 0 additions & 15 deletions
This file was deleted.

compiler/plc_lowering/src/inheritance.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ impl AstVisitorMut for InheritanceLowerer {
192192

193193
let block = VariableBlock {
194194
variables: vec![base_var],
195-
variable_block_type: VariableBlockType::Local,
195+
kind: VariableBlockType::Local,
196196
linkage: LinkageType::Internal,
197197
location: SourceLocation::internal(),
198198
..Default::default()
@@ -2141,6 +2141,7 @@ mod units_tests {
21412141
],
21422142
pou_type: Method {
21432143
parent: "child",
2144+
property: None,
21442145
declaration_kind: Concrete,
21452146
},
21462147
return_type: None,
@@ -2168,13 +2169,14 @@ mod units_tests {
21682169

21692170
let (_, project) = parse_and_annotate("test", vec![src]).unwrap();
21702171
let unit = &project.units[0].get_unit().implementations[1];
2171-
assert_debug_snapshot!(unit, @r#"
2172+
assert_debug_snapshot!(unit, @r###"
21722173
Implementation {
21732174
name: "bar.set0",
21742175
type_name: "bar.set0",
21752176
linkage: Internal,
21762177
pou_type: Method {
21772178
parent: "bar",
2179+
property: None,
21782180
declaration_kind: Concrete,
21792181
},
21802182
statements: [
@@ -2255,7 +2257,7 @@ mod units_tests {
22552257
Protected,
22562258
),
22572259
}
2258-
"#);
2260+
"###);
22592261
}
22602262
}
22612263

@@ -2387,7 +2389,7 @@ mod inherited_properties {
23872389

23882390
let (_, AnnotatedProject { units, .. }) = parse_and_annotate("test", vec![src]).unwrap();
23892391
let implementation = &units[0].get_unit().implementations[1];
2390-
let stmt = &dbg!(implementation).statements[0];
2392+
let stmt = &implementation.statements[0];
23912393
assert_debug_snapshot!(stmt, @r###"
23922394
CallStatement {
23932395
operator: ReferenceExpr {
@@ -2433,7 +2435,7 @@ mod inherited_properties {
24332435

24342436
let (_, AnnotatedProject { units, .. }) = parse_and_annotate("test", vec![src]).unwrap();
24352437
let implementation = &units[0].get_unit().implementations[2];
2436-
let stmt = &dbg!(implementation).statements[0];
2438+
let stmt = &implementation.statements[0];
24372439
assert_debug_snapshot!(stmt, @r###"
24382440
CallStatement {
24392441
operator: ReferenceExpr {
@@ -2491,7 +2493,7 @@ mod inherited_properties {
24912493
.into();
24922494

24932495
let (_, AnnotatedProject { units, .. }) = parse_and_annotate("test", vec![src]).unwrap();
2494-
let implementation = dbg!(&units[0].get_unit().implementations[2]);
2496+
let implementation = &units[0].get_unit().implementations[2];
24952497
let stmt = &implementation.statements[0];
24962498
assert_debug_snapshot!(stmt, @r###"
24972499
CallStatement {

src/codegen/tests/oop_tests.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,3 +534,54 @@ fn complex_array_access_generated() {
534534
}
535535
"###);
536536
}
537+
538+
#[test]
539+
fn properties_are_methods() {
540+
let property = codegen(
541+
"
542+
FUNCTION_BLOCK fb
543+
VAR
544+
localPrivateVariable : DINT;
545+
END_VAR
546+
PROPERTY foo : DINT
547+
GET
548+
foo := localPrivateVariable;
549+
END_GET
550+
551+
SET
552+
localPrivateVariable := foo;
553+
END_SET
554+
END_PROPERTY
555+
END_FUNCTION_BLOCK
556+
",
557+
);
558+
559+
let method = codegen(
560+
"
561+
FUNCTION_BLOCK fb
562+
VAR
563+
localPrivateVariable : DINT;
564+
END_VAR
565+
566+
METHOD __get_foo : DINT
567+
VAR
568+
foo : DINT;
569+
END_VAR
570+
571+
foo := localPrivateVariable;
572+
__get_foo := foo;
573+
END_METHOD
574+
575+
METHOD __set_foo
576+
VAR_INPUT
577+
foo : DINT;
578+
END_VAR
579+
580+
localPrivateVariable := foo;
581+
END_METHOD
582+
END_FUNCTION_BLOCK
583+
",
584+
);
585+
586+
assert_eq!(property, method);
587+
}

0 commit comments

Comments
 (0)