lib.enum: fix shape calculation for const-castable member values.
This commit is contained in:
parent
de36e3c162
commit
7ea2e175e4
|
@ -2,7 +2,6 @@ import enum as py_enum
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
from ..hdl.ast import Shape, ShapeCastable, Const
|
from ..hdl.ast import Shape, ShapeCastable, Const
|
||||||
from .._utils import bits_for
|
|
||||||
|
|
||||||
|
|
||||||
__all__ = py_enum.__all__
|
__all__ = py_enum.__all__
|
||||||
|
@ -38,20 +37,21 @@ class EnumMeta(ShapeCastable, py_enum.EnumMeta):
|
||||||
cls._amaranth_shape_ = shape = Shape.cast(shape)
|
cls._amaranth_shape_ = shape = Shape.cast(shape)
|
||||||
for member in cls:
|
for member in cls:
|
||||||
try:
|
try:
|
||||||
Const.cast(member.value)
|
member_shape = Const.cast(member.value).shape()
|
||||||
except TypeError as e:
|
except TypeError as e:
|
||||||
raise TypeError("Value of enumeration member {!r} must be "
|
raise TypeError("Value of enumeration member {!r} must be "
|
||||||
"a constant-castable expression"
|
"a constant-castable expression"
|
||||||
.format(member)) from e
|
.format(member)) from e
|
||||||
width = bits_for(member.value, shape.signed)
|
if member_shape.signed and not shape.signed:
|
||||||
if member.value < 0 and not shape.signed:
|
|
||||||
warnings.warn(
|
warnings.warn(
|
||||||
message="Value of enumeration member {!r} is signed, but enumeration "
|
message="Value of enumeration member {!r} is signed, but enumeration "
|
||||||
"shape is {!r}" # the repr will be `unsigned(X)`
|
"shape is {!r}" # the repr will be `unsigned(X)`
|
||||||
.format(member, shape),
|
.format(member, shape),
|
||||||
category=RuntimeWarning,
|
category=RuntimeWarning,
|
||||||
stacklevel=2)
|
stacklevel=2)
|
||||||
elif width > shape.width:
|
elif (member_shape.width > shape.width or
|
||||||
|
member_shape.width == shape.width and
|
||||||
|
shape.signed and not member_shape.signed):
|
||||||
warnings.warn(
|
warnings.warn(
|
||||||
message="Value of enumeration member {!r} will be truncated to "
|
message="Value of enumeration member {!r} will be truncated to "
|
||||||
"enumeration shape {!r}"
|
"enumeration shape {!r}"
|
||||||
|
|
Loading…
Reference in a new issue