diff --git a/pdl-compiler/scripts/generate_python_backend.py b/pdl-compiler/scripts/generate_python_backend.py index 014561f..8938173 100755 --- a/pdl-compiler/scripts/generate_python_backend.py +++ b/pdl-compiler/scripts/generate_python_backend.py @@ -904,9 +904,10 @@ def generate_packet_parser(packet: ast.Declaration) -> List[str]: # Convert the packet constraints to a boolean expression. validation = [] - if packet.constraints: + constraints = core.get_all_packet_constraints(packet) + if constraints: cond = [] - for c in packet.constraints: + for c in constraints: if c.value is not None: cond.append(f"fields['{c.id}'] != {hex(c.value)}") else: @@ -989,11 +990,7 @@ def generate_packet_post_init(decl: ast.Declaration) -> List[str]: """Generate __post_init__ function to set constraint field values.""" # Gather all constraints from parent packets. - constraints = [] - current = decl - while current.parent_id: - constraints.extend(current.constraints) - current = current.parent + constraints = core.get_all_packet_constraints(decl) if constraints: code = [] diff --git a/pdl-compiler/scripts/pdl/core.py b/pdl-compiler/scripts/pdl/core.py index 16a97b1..5ddbb02 100644 --- a/pdl-compiler/scripts/pdl/core.py +++ b/pdl-compiler/scripts/pdl/core.py @@ -182,6 +182,19 @@ def get_derived_packets( return children +def get_all_packet_constraints( + decl: Union[PacketDeclaration, StructDeclaration] +) -> List[Constraint]: + """Return the list of constraints defined in the selected packet and + its parent declarations.""" + + constraints = [] + while decl.parent_id: + constraints.extend(decl.constraints) + decl = decl.parent + return constraints + + def get_field_size(field: Field, skip_payload: bool = False) -> Optional[int]: """Determine the size of a field in bits, if possible. If the field is dynamically sized (e.g. unsized array or payload field),