Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions compiler/back_end/cpp/header_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ def _get_cpp_view_type_for_type_definition(
adapted_buffer_type = _get_adapted_cpp_buffer_type_for_field(
type_definition, size, buffer_type, byte_order, parent_addressable_unit
)
if type_definition.HasField("external"):
if type_definition.has_field("external"):
# Externals do not (yet) support runtime parameters.
return (
code_template.format_template(
Expand All @@ -433,7 +433,7 @@ def _get_cpp_view_type_for_type_definition(
),
[],
)
elif type_definition.HasField("structure"):
elif type_definition.has_field("structure"):
parameter_types = []
for parameter in type_definition.runtime_parameter:
parameter_types.append(
Expand All @@ -450,7 +450,7 @@ def _get_cpp_view_type_for_type_definition(
),
parameter_types,
)
elif type_definition.HasField("enumeration"):
elif type_definition.has_field("enumeration"):
return (
code_template.format_template(
_TEMPLATES.enum_view_type,
Expand Down Expand Up @@ -534,7 +534,7 @@ def _get_cpp_view_type_for_physical_type(
element_view_parameters,
)
else:
assert type_ir.HasField("atomic_type")
assert type_ir.has_field("atomic_type")
reference = type_ir.atomic_type.reference
referenced_type = ir_util.find_object(reference, ir)
if parent_addressable_unit > referenced_type.addressable_unit:
Expand Down Expand Up @@ -829,13 +829,13 @@ def _render_expression(expression, ir, field_reader=None, subexpressions=None):
True,
)
elif expression.type.which_type == "boolean":
if expression.type.boolean.HasField("value"):
if expression.type.boolean.has_field("value"):
if expression.type.boolean.value:
return _ExpressionResult(_maybe_type("bool") + "(true)", True)
else:
return _ExpressionResult(_maybe_type("bool") + "(false)", True)
elif expression.type.which_type == "enumeration":
if expression.type.enumeration.HasField("value"):
if expression.type.enumeration.has_field("value"):
return _ExpressionResult(
_render_enum_value(expression.type.enumeration, ir), True
)
Expand Down Expand Up @@ -889,7 +889,7 @@ def _get_cpp_type_reader_of_field(
):
"""Returns the C++ view type for a field."""
field_size = None
if field_ir.type.HasField("size_in_bits"):
if field_ir.type.has_field("size_in_bits"):
field_size = ir_util.constant_value(field_ir.type.size_in_bits)
assert field_size is not None
elif ir_util.is_constant(field_ir.location.size):
Expand Down Expand Up @@ -1365,7 +1365,7 @@ def _generate_structure_definition(type_ir, ir, config: Config):
units = {1: "Bits", 8: "Bytes"}[type_ir.addressable_unit]

for subtype in type_ir.subtype:
if subtype.HasField("enumeration"):
if subtype.has_field("enumeration"):
enum_using_statements.append(
code_template.format_template(
_TEMPLATES.enum_using_statement,
Expand Down Expand Up @@ -1672,11 +1672,11 @@ def _generate_enum_definition(type_ir, include_traits=True):

def _generate_type_definition(type_ir, ir, config: Config):
"""Generates C++ for an Emboss type."""
if type_ir.HasField("structure"):
if type_ir.has_field("structure"):
return _generate_structure_definition(type_ir, ir, config)
elif type_ir.HasField("enumeration"):
elif type_ir.has_field("enumeration"):
return _generate_enum_definition(type_ir, config.include_enum_traits)
elif type_ir.HasField("external"):
elif type_ir.has_field("external"):
# TODO(bolms): This should probably generate an #include.
return "", "", ""
else:
Expand Down
4 changes: 2 additions & 2 deletions compiler/front_end/attribute_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def _fixed_size_of_struct_or_bits(struct, unit_size):
"""Returns size of struct in bits or None, if struct is not fixed size."""
size = 0
for field in struct.field:
if not field.HasField("location"):
if not field.has_field("location"):
# Virtual fields do not contribute to the physical size of the struct.
continue
field_start = ir_util.constant_value(field.location.start)
Expand Down Expand Up @@ -421,7 +421,7 @@ def _verify_requires_attribute_on_field(field, source_file_name, ir, errors):
if ir_util.field_is_virtual(field):
field_expression_type = field.read_transform.type
else:
if not field.type.HasField("atomic_type"):
if not field.type.has_field("atomic_type"):
errors.append(
[
error.error(
Expand Down
24 changes: 12 additions & 12 deletions compiler/front_end/attribute_checker_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,12 +514,12 @@ def test_adds_byte_order_attributes_from_default(self):
byte_order_attr = ir_util.get_attribute(
ir.module[0].type[0].structure.field[0].attribute, _BYTE_ORDER
)
self.assertTrue(byte_order_attr.HasField("string_constant"))
self.assertTrue(byte_order_attr.has_field("string_constant"))
self.assertEqual("BigEndian", byte_order_attr.string_constant.text)
byte_order_attr = ir_util.get_attribute(
ir.module[0].type[0].structure.field[1].attribute, _BYTE_ORDER
)
self.assertTrue(byte_order_attr.HasField("string_constant"))
self.assertTrue(byte_order_attr.has_field("string_constant"))
self.assertEqual("LittleEndian", byte_order_attr.string_constant.text)

def test_adds_null_byte_order_attributes(self):
Expand All @@ -537,28 +537,28 @@ def test_adds_null_byte_order_attributes(self):
byte_order_attr = ir_util.get_attribute(
structure.field[0].attribute, _BYTE_ORDER
)
self.assertTrue(byte_order_attr.HasField("string_constant"))
self.assertTrue(byte_order_attr.has_field("string_constant"))
self.assertEqual("Null", byte_order_attr.string_constant.text)
self.assertEqual(
structure.field[0].source_location, byte_order_attr.source_location
)
byte_order_attr = ir_util.get_attribute(
structure.field[1].attribute, _BYTE_ORDER
)
self.assertTrue(byte_order_attr.HasField("string_constant"))
self.assertTrue(byte_order_attr.has_field("string_constant"))
self.assertEqual("LittleEndian", byte_order_attr.string_constant.text)
byte_order_attr = ir_util.get_attribute(
structure.field[2].attribute, _BYTE_ORDER
)
self.assertTrue(byte_order_attr.HasField("string_constant"))
self.assertTrue(byte_order_attr.has_field("string_constant"))
self.assertEqual("Null", byte_order_attr.string_constant.text)
self.assertEqual(
structure.field[2].source_location, byte_order_attr.source_location
)
byte_order_attr = ir_util.get_attribute(
structure.field[3].attribute, _BYTE_ORDER
)
self.assertTrue(byte_order_attr.HasField("string_constant"))
self.assertTrue(byte_order_attr.has_field("string_constant"))
self.assertEqual("LittleEndian", byte_order_attr.string_constant.text)

def test_disallows_default_byte_order_on_field(self):
Expand Down Expand Up @@ -631,7 +631,7 @@ def test_adds_byte_order_from_scoped_default(self):
byte_order_attr = ir_util.get_attribute(
ir.module[0].type[0].structure.field[0].attribute, _BYTE_ORDER
)
self.assertTrue(byte_order_attr.HasField("string_constant"))
self.assertTrue(byte_order_attr.has_field("string_constant"))
self.assertEqual("BigEndian", byte_order_attr.string_constant.text)

def test_disallows_unknown_byte_order(self):
Expand Down Expand Up @@ -926,39 +926,39 @@ def test_adds_false_is_signed_attribute(self):
self.assertEqual([], attribute_checker.normalize_and_verify(ir))
enum = ir.module[0].type[0]
is_signed_attr = ir_util.get_attribute(enum.attribute, _IS_SIGNED)
self.assertTrue(is_signed_attr.expression.HasField("boolean_constant"))
self.assertTrue(is_signed_attr.expression.has_field("boolean_constant"))
self.assertFalse(is_signed_attr.expression.boolean_constant.value)

def test_leaves_is_signed_attribute(self):
ir = _make_ir_from_emb("enum Foo:\n" " [is_signed: true]\n" " ZERO = 0\n")
self.assertEqual([], attribute_checker.normalize_and_verify(ir))
enum = ir.module[0].type[0]
is_signed_attr = ir_util.get_attribute(enum.attribute, _IS_SIGNED)
self.assertTrue(is_signed_attr.expression.HasField("boolean_constant"))
self.assertTrue(is_signed_attr.expression.has_field("boolean_constant"))
self.assertTrue(is_signed_attr.expression.boolean_constant.value)

def test_adds_true_is_signed_attribute(self):
ir = _make_ir_from_emb("enum Foo:\n" " NEGATIVE_ONE = -1\n")
self.assertEqual([], attribute_checker.normalize_and_verify(ir))
enum = ir.module[0].type[0]
is_signed_attr = ir_util.get_attribute(enum.attribute, _IS_SIGNED)
self.assertTrue(is_signed_attr.expression.HasField("boolean_constant"))
self.assertTrue(is_signed_attr.expression.has_field("boolean_constant"))
self.assertTrue(is_signed_attr.expression.boolean_constant.value)

def test_adds_max_bits_attribute(self):
ir = _make_ir_from_emb("enum Foo:\n" " ZERO = 0\n")
self.assertEqual([], attribute_checker.normalize_and_verify(ir))
enum = ir.module[0].type[0]
max_bits_attr = ir_util.get_attribute(enum.attribute, _MAX_BITS)
self.assertTrue(max_bits_attr.expression.HasField("constant"))
self.assertTrue(max_bits_attr.expression.has_field("constant"))
self.assertEqual("64", max_bits_attr.expression.constant.value)

def test_leaves_max_bits_attribute(self):
ir = _make_ir_from_emb("enum Foo:\n" " [maximum_bits: 32]\n" " ZERO = 0\n")
self.assertEqual([], attribute_checker.normalize_and_verify(ir))
enum = ir.module[0].type[0]
max_bits_attr = ir_util.get_attribute(enum.attribute, _MAX_BITS)
self.assertTrue(max_bits_attr.expression.HasField("constant"))
self.assertTrue(max_bits_attr.expression.has_field("constant"))
self.assertEqual("32", max_bits_attr.expression.constant.value)

def test_rejects_too_small_max_bits(self):
Expand Down
32 changes: 16 additions & 16 deletions compiler/front_end/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@

def _render_type(type_ir, ir):
"""Returns the human-readable notation of the given type."""
assert type_ir.HasField(
assert type_ir.has_field(
"atomic_type"
), "TODO(bolms): Implement _render_type for array types."
if type_ir.HasField("size_in_bits"):
if type_ir.has_field("size_in_bits"):
return _render_atomic_type_name(
type_ir, ir, suffix=":" + str(ir_util.constant_value(type_ir.size_in_bits))
)
Expand All @@ -37,7 +37,7 @@ def _render_type(type_ir, ir):


def _render_atomic_type_name(type_ir, ir, suffix=None):
assert type_ir.HasField(
assert type_ir.has_field(
"atomic_type"
), "_render_atomic_type_name() requires an atomic type"
if not suffix:
Expand Down Expand Up @@ -78,16 +78,16 @@ def _check_that_inner_array_dimensions_are_constant(type_ir, source_file_name, e

def _check_that_array_base_types_are_fixed_size(type_ir, source_file_name, errors, ir):
"""Checks that the sizes of array elements are known at compile time."""
if type_ir.base_type.HasField("array_type"):
if type_ir.base_type.has_field("array_type"):
# An array is fixed size if its base_type is fixed size and its array
# dimension is constant. This function will be called again on the inner
# array, and we do not want to cascade errors if the inner array's base_type
# is not fixed size. The array dimensions are separately checked by
# _check_that_inner_array_dimensions_are_constant, which will provide an
# appropriate error message for that case.
return
assert type_ir.base_type.HasField("atomic_type")
if type_ir.base_type.HasField("size_in_bits"):
assert type_ir.base_type.has_field("atomic_type")
if type_ir.base_type.has_field("size_in_bits"):
# If the base_type has a size_in_bits, then it is fixed size.
return
base_type = ir_util.find_object(type_ir.base_type.atomic_type.reference, ir)
Expand All @@ -111,11 +111,11 @@ def _check_that_array_base_types_in_structs_are_multiples_of_bytes(
):
# TODO(bolms): Remove this limitation.
"""Checks that the sizes of array elements are multiples of 8 bits."""
if type_ir.base_type.HasField("array_type"):
if type_ir.base_type.has_field("array_type"):
# Only check the innermost array for multidimensional arrays.
return
assert type_ir.base_type.HasField("atomic_type")
if type_ir.base_type.HasField("size_in_bits"):
assert type_ir.base_type.has_field("atomic_type")
if type_ir.base_type.has_field("size_in_bits"):
assert ir_util.is_constant(type_ir.base_type.size_in_bits)
base_type_size = ir_util.constant_value(type_ir.base_type.size_in_bits)
else:
Expand Down Expand Up @@ -209,10 +209,10 @@ def _check_type_requirements_for_field(
type_ir, type_definition, field, ir, source_file_name, errors
):
"""Checks that the `requires` attribute of each field's type is fulfilled."""
if not type_ir.HasField("atomic_type"):
if not type_ir.has_field("atomic_type"):
return

if field.type.HasField("atomic_type"):
if field.type.has_field("atomic_type"):
field_min_size = (
int(field.location.size.type.integer.minimum_value)
* type_definition.addressable_unit
Expand All @@ -225,7 +225,7 @@ def _check_type_requirements_for_field(
else:
field_is_atomic = False

if type_ir.HasField("size_in_bits"):
if type_ir.has_field("size_in_bits"):
element_size = ir_util.constant_value(type_ir.size_in_bits)
else:
element_size = None
Expand Down Expand Up @@ -333,7 +333,7 @@ def _check_early_type_requirements_for_parameter_type(
# bits by default) in expressions, so the physical size would just be
# ignored. Integer types do not have "natural" sizes, so the width is
# required.
if not physical_type.HasField("size_in_bits"):
if not physical_type.has_field("size_in_bits"):
errors.extend(
[
[
Expand All @@ -346,7 +346,7 @@ def _check_early_type_requirements_for_parameter_type(
]
)
elif logical_type.which_type == "enumeration":
if physical_type.HasField("size_in_bits"):
if physical_type.has_field("size_in_bits"):
errors.extend(
[
[
Expand Down Expand Up @@ -399,7 +399,7 @@ def _check_physical_type_requirements(
):
"""Checks that the given atomic `type_ir` is allowed to be `size` bits."""
referenced_type_definition = ir_util.find_object(type_ir.atomic_type.reference, ir)
if referenced_type_definition.HasField("enumeration"):
if referenced_type_definition.has_field("enumeration"):
if size is None:
return [
[
Expand Down Expand Up @@ -465,7 +465,7 @@ def _check_physical_type_requirements(

def _check_allowed_in_bits(type_ir, type_definition, source_file_name, ir, errors):
"""Verifies that atomic fields have types that are allowed in `bits`."""
if not type_ir.HasField("atomic_type"):
if not type_ir.has_field("atomic_type"):
return
referenced_type_definition = ir_util.find_object(type_ir.atomic_type.reference, ir)
if (
Expand Down
6 changes: 3 additions & 3 deletions compiler/front_end/expression_bounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def _compute_constraints_of_field_reference(expression, ir):
referrent_type = field.type
else:
referrent_type = field.physical_type_alias
if referrent_type.HasField("size_in_bits"):
if referrent_type.has_field("size_in_bits"):
type_size = ir_util.constant_value(referrent_type.size_in_bits)
elif isinstance(field, ir_data.Field):
field_size = ir_util.constant_value(field.location.size)
Expand All @@ -159,7 +159,7 @@ def _compute_constraints_of_field_reference(expression, ir):
type_size = field_size * type_definition.addressable_unit
else:
type_size = None
assert referrent_type.HasField("atomic_type"), field
assert referrent_type.has_field("atomic_type"), field
assert not referrent_type.atomic_type.reference.canonical_name.module_file
_set_integer_constraints_from_physical_type(
expression, referrent_type, type_size
Expand Down Expand Up @@ -668,7 +668,7 @@ def _compute_constraints_of_choice_operator(expression):
"""Computes the constraints of a choice operation '?:'."""
condition, if_true, if_false = ir_data_utils.reader(expression).function.args
expression = ir_data_utils.builder(expression)
if condition.type.boolean.HasField("value"):
if condition.type.boolean.has_field("value"):
# The generated expressions for $size_in_bits and $size_in_bytes look like
#
# $max((field1_existence_condition ? field1_start + field1_size : 0),
Expand Down
Loading