hdl.ir: type check ports.

Fixes #290.
This commit is contained in:
whitequark 2020-02-06 17:33:41 +00:00
parent 882fddfa96
commit 97cc78a3db
2 changed files with 19 additions and 5 deletions

View file

@ -532,13 +532,22 @@ class Fragment:
if ports is None: if ports is None:
fragment._propagate_ports(ports=(), all_undef_as_ports=True) fragment._propagate_ports(ports=(), all_undef_as_ports=True)
else: else:
ports = map(DomainLowerer(fragment.domains).on_value, ports) mapped_ports = []
new_ports = [] # Lower late bound signals like ClockSignal() to ports.
port_lowerer = DomainLowerer(fragment.domains)
for port in ports:
if not isinstance(port, (Signal, ClockSignal, ResetSignal)):
raise TypeError("Only signals may be added as ports, not {!r}"
.format(port))
mapped_ports.append(port_lowerer.on_value(port))
# Add ports for all newly created missing clock domains, since not doing so defeats
# the purpose of domain auto-creation. (It's possible to refer to these ports before
# the domain actually exists through late binding, but it's inconvenient.)
for cd in new_domains: for cd in new_domains:
new_ports.append(cd.clk) mapped_ports.append(cd.clk)
if cd.rst is not None: if cd.rst is not None:
new_ports.append(cd.rst) mapped_ports.append(cd.rst)
fragment._propagate_ports(ports=(*ports, *new_ports), all_undef_as_ports=False) fragment._propagate_ports(ports=mapped_ports, all_undef_as_ports=False)
return fragment return fragment

View file

@ -298,6 +298,11 @@ class FragmentPortsTestCase(FHDLTestCase):
(sync.rst, "i"), (sync.rst, "i"),
])) ]))
def test_port_wrong(self):
f = Fragment()
with self.assertRaises(TypeError,
msg="Only signals may be added as ports, not (const 1'd1)"):
f.prepare(ports=(Const(1),))
class FragmentDomainsTestCase(FHDLTestCase): class FragmentDomainsTestCase(FHDLTestCase):
def test_iter_signals(self): def test_iter_signals(self):