Skip to content

Commit dfef3bc

Browse files
wanda-phiwhitequark
authored andcommitted
hdl: make Const(enum_value) work.
Fixes #1413.
1 parent 3cbe4be commit dfef3bc

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

amaranth/hdl/_ast.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,6 +1598,10 @@ def cast(obj):
15981598

15991599
def __init__(self, value, shape=None, *, src_loc_at=0):
16001600
# We deliberately do not call Value.__init__ here.
1601+
if isinstance(value, Enum):
1602+
if shape is None:
1603+
shape = Shape.cast(type(value))
1604+
value = value.value
16011605
value = int(operator.index(value))
16021606
if shape is None:
16031607
shape = Shape(bits_for(value), signed=value < 0)

tests/test_hdl_ast.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,20 @@ def test_hash(self):
521521
with self.assertRaises(TypeError):
522522
hash(Const(0))
523523

524+
def test_enum(self):
525+
e1 = Const(UnsignedEnum.FOO)
526+
self.assertIsInstance(e1, Const)
527+
self.assertEqual(e1.shape(), unsigned(2))
528+
e2 = Const(SignedEnum.FOO)
529+
self.assertIsInstance(e2, Const)
530+
self.assertEqual(e2.shape(), signed(2))
531+
e3 = Const(TypedEnum.FOO)
532+
self.assertIsInstance(e3, Const)
533+
self.assertEqual(e3.shape(), unsigned(2))
534+
e4 = Const(UnsignedEnum.FOO, 4)
535+
self.assertIsInstance(e4, Const)
536+
self.assertEqual(e4.shape(), unsigned(4))
537+
524538
def test_shape_castable(self):
525539
class MockConstValue(ValueCastable):
526540
def __init__(self, value):

0 commit comments

Comments
 (0)