hdl.rec: make Record inherit from UserValue.

Closes #354.
This commit is contained in:
anuejn 2020-04-16 18:46:55 +02:00 committed by GitHub
parent b4af217ed0
commit ff6c0327a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 32 additions and 24 deletions

View file

@ -1188,7 +1188,10 @@ class UserValue(Value):
def _lazy_lower(self):
if self.__lowered is None:
self.__lowered = Value.cast(self.lower())
lowered = self.lower()
if isinstance(lowered, UserValue):
lowered = lowered._lazy_lower()
self.__lowered = Value.cast(lowered)
return self.__lowered
def shape(self):

View file

@ -85,7 +85,7 @@ class Layout:
# Unlike most Values, Record *can* be subclassed.
class Record(Value):
class Record(UserValue):
@staticmethod
def like(other, *, name=None, name_suffix=None, src_loc_at=0):
if name is not None:
@ -113,6 +113,8 @@ class Record(Value):
return Record(other.layout, name=new_name, fields=fields, src_loc_at=1)
def __init__(self, layout, *, name=None, fields=None, src_loc_at=0):
super().__init__(src_loc_at=src_loc_at)
if name is None:
name = tracer.get_var_name(depth=2 + src_loc_at, default=None)
@ -165,8 +167,8 @@ class Record(Value):
else:
return super().__getitem__(item)
def shape(self):
return Shape(sum(len(f) for f in self.fields.values()))
def lower(self):
return Cat(self.fields.values())
def _lhs_signals(self):
return union((f._lhs_signals() for f in self.fields.values()), start=SignalSet())

View file

@ -38,10 +38,6 @@ class ValueVisitor(metaclass=ABCMeta):
def on_Signal(self, value):
pass # :nocov:
@abstractmethod
def on_Record(self, value):
pass # :nocov:
@abstractmethod
def on_ClockSignal(self, value):
pass # :nocov:
@ -98,9 +94,6 @@ class ValueVisitor(metaclass=ABCMeta):
elif isinstance(value, Signal):
# Uses `isinstance()` and not `type() is` because nmigen.compat requires it.
new_value = self.on_Signal(value)
elif isinstance(value, Record):
# Uses `isinstance()` and not `type() is` to allow inheriting from Record.
new_value = self.on_Record(value)
elif type(value) is ClockSignal:
new_value = self.on_ClockSignal(value)
elif type(value) is ResetSignal:
@ -147,9 +140,6 @@ class ValueTransformer(ValueVisitor):
def on_Signal(self, value):
return value
def on_Record(self, value):
return value
def on_ClockSignal(self, value):
return value
@ -372,8 +362,6 @@ class DomainCollector(ValueVisitor, StatementVisitor):
def on_ResetSignal(self, value):
self._add_used_domain(value.domain)
on_Record = on_ignore
def on_Operator(self, value):
for o in value.operands:
self.on_value(o)