hdl.rec: allow using Enum subclass as shape.

Fixes #223.
This commit is contained in:
whitequark 2019-09-22 15:16:36 +00:00
parent 6414c80b82
commit 4c582ef609
2 changed files with 17 additions and 0 deletions

View file

@ -5,6 +5,7 @@ from functools import reduce
from .. import tracer
from ..tools import union
from .ast import *
from .ast import _enum_shape
__all__ = ["Direction", "DIR_NONE", "DIR_FANOUT", "DIR_FANIN", "Layout", "Record"]
@ -34,6 +35,8 @@ class Layout:
if len(field) == 2:
name, shape = field
direction = DIR_NONE
if isinstance(shape, type) and issubclass(shape, Enum):
shape = _enum_shape(shape)
if isinstance(shape, list):
shape = Layout.wrap(shape)
else:

View file

@ -1,8 +1,16 @@
from enum import Enum
from ..hdl.ast import *
from ..hdl.rec import *
from .tools import *
class UnsignedEnum(Enum):
FOO = 1
BAR = 2
BAZ = 3
class LayoutTestCase(FHDLTestCase):
def test_fields(self):
layout = Layout.wrap([
@ -25,6 +33,12 @@ class LayoutTestCase(FHDLTestCase):
self.assertEqual(sublayout["a"], ((1, False), DIR_NONE))
self.assertEqual(sublayout["b"], ((1, False), DIR_NONE))
def test_enum_field(self):
layout = Layout.wrap([
("enum", UnsignedEnum),
])
self.assertEqual(layout["enum"], ((2, False), DIR_NONE))
def test_slice_tuple(self):
layout = Layout.wrap([
("a", 1),