diff --git a/rflx/generator/generator.py b/rflx/generator/generator.py index 0ff96b39b..059f408fc 100644 --- a/rflx/generator/generator.py +++ b/rflx/generator/generator.py @@ -364,12 +364,7 @@ def __create_message(self, message: Message) -> None: sequence_fields[f] = t if isinstance(t, Opaque): opaque_fields.append(f) - if any( - bool( - l.size.findall(lambda x: x in [expr.Size("Message"), expr.Last("Message")]) - ) - for l in message.incoming(f) - ): + if any(l.has_implicit_size for l in message.incoming(f)): fields_with_implicit_size.append(f) else: fields_with_explicit_size.append(f) diff --git a/rflx/generator/serializer.py b/rflx/generator/serializer.py index 820961b59..00b1b5844 100644 --- a/rflx/generator/serializer.py +++ b/rflx/generator/serializer.py @@ -64,7 +64,7 @@ ) from rflx.common import unique from rflx.const import BUILTINS_PACKAGE -from rflx.model import FINAL, Enumeration, Field, Link, Message, Opaque, Scalar, Sequence, Type +from rflx.model import FINAL, Enumeration, Field, Message, Opaque, Scalar, Sequence, Type from . import common, const @@ -264,11 +264,6 @@ def substituted(expression: expr.Expr) -> Expr: .ada_expr() ) - def has_implicit_size(link: Link) -> bool: - return bool( - link.size.findall(lambda x: x in [expr.Size("Message"), expr.Last("Message")]) - ) - target_links = [ (target, list(links)) for target, links in itertools.groupby(message.outgoing(field), lambda x: x.target) @@ -297,13 +292,13 @@ def has_implicit_size(link: Link) -> bool: size = explicit_size else: if len(links) == 1: - size = implicit_size if has_implicit_size(links[0]) else explicit_size + size = implicit_size if links[0].has_implicit_size else explicit_size else: size = If( [ ( substituted(l.condition), - implicit_size if has_implicit_size(l) else explicit_size, + implicit_size if l.has_implicit_size else explicit_size, ) for l in links ], diff --git a/rflx/model/message.py b/rflx/model/message.py index c939703a8..92abc4e39 100644 --- a/rflx/model/message.py +++ b/rflx/model/message.py @@ -81,6 +81,10 @@ def __eq__(self, other: object) -> bool: def __hash__(self) -> int: return 0 + @property + def has_implicit_size(self) -> bool: + return bool(self.size.findall(lambda x: x in [expr.Size("Message"), expr.Last("Message")])) + def valid_message_field_types(message: "AbstractMessage") -> bool: for t in message.types.values(): @@ -630,7 +634,7 @@ def _validate_link_aspects(self) -> None: ] ) - if link.size.findall(lambda x: x in {expr.Size("Message"), expr.Last("Message")}): + if link.has_implicit_size: if any(l.target != FINAL for l in self.outgoing(link.target)): self.error.extend( [ @@ -868,15 +872,7 @@ def has_fixed_size(self) -> bool: @property def has_implicit_size(self) -> bool: - """ - Return true if the message has a implicit size. - - All messages containing a reference to `Message` in a size aspect are considered as - having a implicit size. - """ - return all( - v.identifier != ID("Message") for l in self.structure for v in l.size.variables() - ) + return any(l.has_implicit_size for l in self.structure) @property def is_definite(self) -> bool: @@ -888,7 +884,7 @@ def is_definite(self) -> bool: """ return ( len(self.paths(FINAL)) <= 1 - and self.has_implicit_size + and not self.has_implicit_size and all( not l.size.findall(lambda x: isinstance(x, (expr.First, expr.Last))) for l in self.structure @@ -1016,7 +1012,7 @@ def max_size(self) -> expr.Number: if not self.structure: return expr.Number(0) - if not self.has_implicit_size: + if self.has_implicit_size: fail( "unable to calculate maximum size of message with implicit size", Subsystem.MODEL, @@ -1035,7 +1031,7 @@ def max_field_sizes(self) -> Dict[Field, expr.Number]: if not self.structure: return {} - if not self.has_implicit_size: + if self.has_implicit_size: fail( "unable to calculate maximum field sizes of message with implicit size", Subsystem.MODEL, diff --git a/tests/unit/model/message_test.py b/tests/unit/model/message_test.py index 5d5b13c7a..e17be6ad8 100644 --- a/tests/unit/model/message_test.py +++ b/tests/unit/model/message_test.py @@ -2672,11 +2672,11 @@ def test_has_fixed_size() -> None: def test_has_implicit_size() -> None: - assert NULL_MESSAGE.has_implicit_size - assert FIXED_SIZE_MESSAGE.has_implicit_size - assert TLV_MESSAGE.has_implicit_size - assert not ETHERNET_FRAME.has_implicit_size - assert SEQUENCE_MESSAGE.has_implicit_size + assert not NULL_MESSAGE.has_implicit_size + assert not FIXED_SIZE_MESSAGE.has_implicit_size + assert not TLV_MESSAGE.has_implicit_size + assert ETHERNET_FRAME.has_implicit_size + assert not SEQUENCE_MESSAGE.has_implicit_size def test_is_definite() -> None: