hdl._ast: add enum name argument to Format.Enum.
Turns out that RTLIL enum representation requires such, so add a place to store it.
This commit is contained in:
parent
1fdd9bf4e9
commit
67f5b61bcc
4 changed files with 23 additions and 6 deletions
|
|
@ -2769,8 +2769,11 @@ class Format(_FormatLike):
|
|||
|
||||
|
||||
class Enum(_FormatLike):
|
||||
def __init__(self, value, /, variants):
|
||||
def __init__(self, value, /, variants, *, name=None):
|
||||
self._value = Value.cast(value)
|
||||
if name is not None and not isinstance(name, str):
|
||||
raise TypeError(f"Enum name must be a string or None, not {name!r}")
|
||||
self._name = name
|
||||
if isinstance(variants, EnumMeta):
|
||||
self._variants = {Const.cast(member.value).value: member.name for member in variants}
|
||||
else:
|
||||
|
|
@ -2796,7 +2799,8 @@ class Format(_FormatLike):
|
|||
f" ({val!r} {name!r})"
|
||||
for val, name in self._variants.items()
|
||||
)
|
||||
return f"(format-enum {self._value!r}{variants})"
|
||||
name = "-" if self._name is None else repr(self._name)
|
||||
return f"(format-enum {self._value!r} {name}{variants})"
|
||||
|
||||
|
||||
class Struct(_FormatLike):
|
||||
|
|
|
|||
|
|
@ -179,7 +179,7 @@ class EnumType(ShapeCastable, py_enum.EnumMeta):
|
|||
def format(cls, value, format_spec):
|
||||
if format_spec != "":
|
||||
raise ValueError(f"Format specifier {format_spec!r} is not supported for enums")
|
||||
return Format.Enum(value, cls)
|
||||
return Format.Enum(value, cls, name=cls.__name__)
|
||||
|
||||
def _value_repr(cls, value):
|
||||
yield Repr(FormatEnum(cls), value)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue