def context_cursor_unchanged( message: model.Message, field: model.Field, predecessors: bool ) -> List[Expr]: lower: model.Field upper: model.Field if predecessors: if len(message.predecessors(field)) == 0: return [] lower = message.fields[0] upper = message.fields[message.fields.index(field) - 1] else: if len(message.successors(field)) == 0: return [] lower = message.fields[message.fields.index(field) + 1] upper = message.fields[-1] return [ ForAllIn( "F", ValueRange( lower=Variable(lower.affixed_name), upper=Variable(upper.affixed_name), type_identifier=ID("Field"), ), Equal( Call( "Context_Cursors_Index", [ Call( "Context_Cursors", [Variable("Ctx")], ), Variable("F"), ], ), Call( "Context_Cursors_Index", [ Old( Call( "Context_Cursors", [Variable("Ctx")], ) ), Variable("F"), ], ), ), ) ]
def create_scalar_setter_procedures( self, message: Message, scalar_fields: Mapping[Field, Scalar] ) -> UnitPart: def specification(field: Field, field_type: Type) -> ProcedureSpecification: type_name = ( field_type.enum_name if isinstance(field_type, Enumeration) and field_type.always_valid else field_type.name ) return ProcedureSpecification( f"Set_{field.name}", [ InOutParameter(["Ctx"], "Context"), Parameter(["Val"], f"{message.package}.{type_name}"), ], ) return UnitPart( [ SubprogramDeclaration( specification(f, t), [ Precondition( AndThen( *self.setter_preconditions(f), Call( "Field_Condition", [ Name("Ctx"), Aggregate( Name(f.affixed_name), Name("Val") if not isinstance(t, Enumeration) else Call("Convert", [Name("Val")]), ), ], ), Call("Valid", [Name("Val")]) if not isinstance(t, Enumeration) else TRUE, self.common.sufficient_space_for_field_condition( Name(f.affixed_name) ), ) ), Postcondition( And( VALID_CONTEXT, Call("Has_Buffer", [Name("Ctx")]), Call("Valid", [Name("Ctx"), Name(f.affixed_name)]), Equal( Call(f"Get_{f.name}", [Name("Ctx")]), Aggregate(TRUE, Name("Val")) if isinstance(t, Enumeration) and t.always_valid else Name("Val"), ), *self.setter_postconditions(message, f, t), *[ Equal( Call("Cursor", [Name("Ctx"), Name(p.affixed_name)]), Old(Call("Cursor", [Name("Ctx"), Name(p.affixed_name)])), ) for p in message.predecessors(f) ], ) ), ], ) for f, t in scalar_fields.items() ], [ SubprogramBody( specification(f, t), [ ObjectDeclaration( ["Field_Value"], "Field_Dependent_Value", Aggregate( Name(f.affixed_name), Name("Val") if not isinstance(t, Enumeration) else Call("Convert", [Name("Val")]), ), True, ), ObjectDeclaration(["First", "Last"], self.types.bit_index), ], [ CallStatement( "Reset_Dependent_Fields", [Name("Ctx"), Name(f.affixed_name)], ), CallStatement( "Set_Field_Value", [Name("Ctx"), Name("Field_Value"), Name("First"), Name("Last")], ), Assignment( "Ctx", Aggregate( Selected("Ctx", "Buffer_First"), Selected("Ctx", "Buffer_Last"), Selected("Ctx", "First"), Name("Last"), Selected("Ctx", "Buffer"), Selected("Ctx", "Cursors"), ), ), Assignment( Indexed(Selected("Ctx", "Cursors"), Name(f.affixed_name)), NamedAggregate( ("State", Name("S_Valid")), ("First", Name("First")), ("Last", Name("Last")), ("Value", Name("Field_Value")), ( "Predecessor", Selected( Indexed(Selected("Ctx", "Cursors"), Name(f.affixed_name)), "Predecessor", ), ), ), ), Assignment( Indexed( Selected("Ctx", "Cursors"), Call("Successor", [Name("Ctx"), Name(f.affixed_name)]), ), NamedAggregate( ("State", Name("S_Invalid")), ("Predecessor", Name(f.affixed_name)), ), ), ], ) for f, t in scalar_fields.items() ], )
def create_scalar_setter_procedures( self, message: Message, scalar_fields: Mapping[Field, Scalar]) -> UnitPart: def specification(field: Field, field_type: Type) -> ProcedureSpecification: if field_type.package == BUILTINS_PACKAGE: type_name = ID(field_type.name) elif isinstance(field_type, Enumeration) and field_type.always_valid: type_name = common.prefixed_type_name( common.full_enum_name(field_type), self.prefix) else: type_name = common.prefixed_type_name(field_type.identifier, self.prefix) return ProcedureSpecification( f"Set_{field.name}", [ InOutParameter(["Ctx"], "Context"), Parameter(["Val"], type_name) ], ) return UnitPart( [ SubprogramDeclaration( specification(f, t), [ Precondition( AndThen( *self.setter_preconditions(f), Call( "Field_Condition", [ Variable("Ctx"), Aggregate( Variable(f.affixed_name), Call("To_Base", [Variable("Val")]), ), ], ), Call("Valid", [Call("To_Base", [Variable("Val")])]) if not isinstance(t, Enumeration) else TRUE, common.sufficient_space_for_field_condition( Variable(f.affixed_name)), )), Postcondition( And( Call("Has_Buffer", [Variable("Ctx")]), Call("Valid", [ Variable("Ctx"), Variable(f.affixed_name) ]), Equal( Call(f"Get_{f.name}", [Variable("Ctx")]), Aggregate(TRUE, Variable("Val")) if isinstance(t, Enumeration) and t.always_valid else Variable("Val"), ), *self.setter_postconditions(message, f), *[ Equal( Call( "Context_Cursor", [ Variable("Ctx"), Variable(p.affixed_name) ], ), Old( Call( "Context_Cursor", [ Variable("Ctx"), Variable(p.affixed_name) ], )), ) for p in message.predecessors(f) ], )), ], ) for f, t in scalar_fields.items() ], [ SubprogramBody( specification(f, t), [ ObjectDeclaration( ["Field_Value"], "Field_Dependent_Value", Aggregate( Variable(f.affixed_name), Call("To_Base", [Variable("Val")]), ), True, ), ObjectDeclaration(["First", "Last"], const.TYPES_BIT_INDEX), ], [ CallStatement( "Reset_Dependent_Fields", [Variable("Ctx"), Variable(f.affixed_name)], ), CallStatement( "Set_Field_Value", [ Variable("Ctx"), Variable("Field_Value"), Variable("First"), Variable("Last"), ], ), Assignment( "Ctx", Aggregate( Variable("Ctx.Buffer_First"), Variable("Ctx.Buffer_Last"), Variable("Ctx.First"), Variable("Last"), Variable("Ctx.Buffer"), Variable("Ctx.Cursors"), ), ), Assignment( Indexed(Variable("Ctx.Cursors"), Variable(f.affixed_name)), NamedAggregate( ("State", Variable("S_Valid")), ("First", Variable("First")), ("Last", Variable("Last")), ("Value", Variable("Field_Value")), ( "Predecessor", Selected( Indexed(Variable("Ctx.Cursors"), Variable(f.affixed_name)), "Predecessor", ), ), ), ), Assignment( Indexed( Variable("Ctx.Cursors"), Call("Successor", [ Variable("Ctx"), Variable(f.affixed_name) ]), ), NamedAggregate( ("State", Variable("S_Invalid")), ("Predecessor", Variable(f.affixed_name)), ), ), ], ) for f, t in scalar_fields.items() ], )