mojo/public/tools/mojom/mojom/generate/module.py
def IsBackwardCompatible(self, older_struct, checker):
    """This struct is backward-compatible with older_struct if and only if all
    of the following conditions hold:
      - Any newly added field is tagged with a [MinVersion] attribute specifying
        a version number greater than all previously used [MinVersion]
        attributes within the struct.
      - All fields present in older_struct remain present in the new struct,
        with the same ordinal position, same optional or non-optional status,
        same (or backward-compatible) type and where applicable, the same
        [MinVersion] attribute value.
      - All [MinVersion] attributes must be non-decreasing in ordinal order.
      - All reference-typed (string, array, map, struct, or union) fields tagged
        with a [MinVersion] greater than zero must be optional.
    """

    def buildOrdinalFieldMap(struct):
      fields_by_ordinal = {}
      for field in struct.fields:
        if field.ordinal in fields_by_ordinal:
          raise Exception('Multiple fields with ordinal %s in struct %s.' %
                          (field.ordinal, struct.mojom_name))
        fields_by_ordinal[field.ordinal] = field
      return fields_by_ordinal

    new_fields = buildOrdinalFieldMap(self)
    old_fields = buildOrdinalFieldMap(older_struct)
    if len(new_fields) < len(old_fields):
      # At least one field was removed, which is not OK.
      return False

    # If there are N fields, existing ordinal values must exactly cover the
    # range from 0 to N-1.
    num_old_ordinals = len(old_fields)
    max_old_min_version = 0
    for ordinal in range(num_old_ordinals):
      new_field = new_fields[ordinal]
      old_field = old_fields[ordinal]
      if (old_field.min_version or 0) > max_old_min_version:
        max_old_min_version = old_field.min_version
      if not _IsFieldBackwardCompatible(new_field, old_field, checker):
        # Type or min-version mismatch between old and new versions of the same
        # ordinal field.
        return False

    # At this point we know all old fields are intact in the new struct
    # definition. Now verify that all new fields have a high enough min version
    # and are appropriately optional where required.
    num_new_ordinals = len(new_fields)
    last_min_version = max_old_min_version
    for ordinal in range(num_old_ordinals, num_new_ordinals):
      new_field = new_fields[ordinal]
      min_version = new_field.min_version or 0
      if min_version <= max_old_min_version:
        # A new field is being added to an existing version, which is not OK.
        return False
      if min_version < last_min_version:
        # The [MinVersion] of a field cannot be lower than the [MinVersion] of
        # a field with lower ordinal value.
        return False
      if IsReferenceKind(new_field.kind) and not IsNullableKind(new_field.kind):
        # New fields whose type can be nullable MUST be nullable.
        return False

    return True
This struct is backward-compatible with older_struct if and only if all
    of the following conditions hold:
      - Any newly added field is tagged with a [MinVersion] attribute specifying
        a version number greater than all previously used [MinVersion]
        attributes within the struct.
      - All fields present in older_struct remain present in the new struct,
        with the same ordinal position, same optional or non-optional status,
        same (or backward-compatible) type and where applicable, the same
        [MinVersion] attribute value.
      - All [MinVersion] attributes must be non-decreasing in ordinal order.
      - All reference-typed (string, array, map, struct, or union) fields tagged
        with a [MinVersion] greater than zero must be optional.
    
This struct is backward-compatible with older_struct if and only if all
of the following conditions hold:
Any newly added field is tagged with a [MinVersion] attribute specifying
a version number greater than all previously used [MinVersion]
attributes within the struct.
All fields present in older_struct remain present in the new struct,
with the same ordinal position, same optional or non-optional status,
same (or backward-compatible) type and where applicable, the same
[MinVersion] attribute value.
All [MinVersion] attributes must be non-decreasing in ordinal order.
All reference-typed (string, array, map, struct, or union) fields tagged
with a [MinVersion] greater than zero must be optional.
This struct is backward - compatible with older_struct if and only if all of the following conditions hold : Any newly added field is tagged with a [ MinVersion ] attribute specifying a version number greater than all previously used [ MinVersion ] attributes within the struct . All fields present in older_struct remain present in the new struct with the same ordinal position same optional or non - optional status same ( or backward - compatible ) type and where applicable the same [ MinVersion ] attribute value . All [ MinVersion ] attributes must be non - decreasing in ordinal order . All reference - typed ( string array map struct or union ) fields tagged with a [ MinVersion ] greater than zero must be optional .
def IsBackwardCompatible(self, older_struct, checker):
    def buildOrdinalFieldMap(struct):
      fields_by_ordinal = {}
      for field in struct.fields:
        if field.ordinal in fields_by_ordinal:
          raise Exception('Multiple fields with ordinal %s in struct %s.' %
                          (field.ordinal, struct.mojom_name))
        fields_by_ordinal[field.ordinal] = field
      return fields_by_ordinal
    new_fields = buildOrdinalFieldMap(self)
    old_fields = buildOrdinalFieldMap(older_struct)
    if len(new_fields) < len(old_fields):
      return False
    num_old_ordinals = len(old_fields)
    max_old_min_version = 0
    for ordinal in range(num_old_ordinals):
      new_field = new_fields[ordinal]
      old_field = old_fields[ordinal]
      if (old_field.min_version or 0) > max_old_min_version:
        max_old_min_version = old_field.min_version
      if not _IsFieldBackwardCompatible(new_field, old_field, checker):
        return False
    num_new_ordinals = len(new_fields)
    last_min_version = max_old_min_version
    for ordinal in range(num_old_ordinals, num_new_ordinals):
      new_field = new_fields[ordinal]
      min_version = new_field.min_version or 0
      if min_version <= max_old_min_version:
        return False
      if min_version < last_min_version:
        return False
      if IsReferenceKind(new_field.kind) and not IsNullableKind(new_field.kind):
        return False
    return True
def IsBackwardCompatible ( self , older_struct , checker ) : def buildOrdinalFieldMap ( struct ) : fields_by_ordinal = { } for field in struct . fields : if field . ordinal in fields_by_ordinal : raise Exception ( 'Multiple fields with ordinal %s in struct %s.' % ( field . ordinal , struct . mojom_name ) ) fields_by_ordinal [ field . ordinal ] = field return fields_by_ordinal new_fields = buildOrdinalFieldMap ( self ) old_fields = buildOrdinalFieldMap ( older_struct ) if len ( new_fields ) < len ( old_fields ) : return False num_old_ordinals = len ( old_fields ) max_old_min_version = 0 for ordinal in range ( num_old_ordinals ) : new_field = new_fields [ ordinal ] old_field = old_fields [ ordinal ] if ( old_field . min_version or 0 ) > max_old_min_version : max_old_min_version = old_field . min_version if not _IsFieldBackwardCompatible ( new_field , old_field , checker ) : return False num_new_ordinals = len ( new_fields ) last_min_version = max_old_min_version for ordinal in range ( num_old_ordinals , num_new_ordinals ) : new_field = new_fields [ ordinal ] min_version = new_field . min_version or 0 if min_version <= max_old_min_version : return False if min_version < last_min_version : return False if IsReferenceKind ( new_field . kind ) and not IsNullableKind ( new_field . kind ) : return False return True
This struct is backward-compatible with older_struct if and only if all
of the following conditions hold:
 Any newly added field is tagged with a [MinVersion] attribute specifying
a version number greater than all previously used [MinVersion]
attributes within the struct.
This struct is backward - compatible with older_struct if and only if all of the following conditions hold : Any newly added field is tagged with a [ MinVersion ] attribute specifying a version number greater than all previously used [ MinVersion ] attributes within the struct .
"""This struct is backward-compatible with older_struct if and only if all
    of the following conditions hold:
      - Any newly added field is tagged with a [MinVersion] attribute specifying
        a version number greater than all previously used [MinVersion]
        attributes within the struct.
      - All fields present in older_struct remain present in the new struct,
        with the same ordinal position, same optional or non-optional status,
        same (or backward-compatible) type and where applicable, the same
        [MinVersion] attribute value.
      - All [MinVersion] attributes must be non-decreasing in ordinal order.
      - All reference-typed (string, array, map, struct, or union) fields tagged
        with a [MinVersion] greater than zero must be optional.
    """ # At least one field was removed, which is not OK. # If there are N fields, existing ordinal values must exactly cover the # range from 0 to N-1. # Type or min-version mismatch between old and new versions of the same # ordinal field. # At this point we know all old fields are intact in the new struct # definition. Now verify that all new fields have a high enough min version # and are appropriately optional where required. # A new field is being added to an existing version, which is not OK. # The [MinVersion] of a field cannot be lower than the [MinVersion] of # a field with lower ordinal value. # New fields whose type can be nullable MUST be nullable.
self
older_struct
checker