Skip to content

Commit

Permalink
hdl.ir: bring new naming rules to instances
Browse files Browse the repository at this point in the history
  • Loading branch information
tpwrules committed Mar 25, 2024
1 parent c175ac8 commit 1cab145
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion amaranth/hdl/_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import enum
import warnings

from .._utils import flatten
from .._utils import flatten, validate_name
from .. import tracer, _unused
from . import _ast, _cd, _ir, _nir

Expand Down Expand Up @@ -309,11 +309,13 @@ class Instance(Fragment):
def __init__(self, type, *args, src_loc=None, src_loc_at=0, **kwargs):
super().__init__(src_loc=src_loc or tracer.get_src_loc(src_loc_at))

validate_name(type, "Instance type")
self.type = type
self.parameters = OrderedDict()
self.named_ports = OrderedDict()

for (kind, name, value) in args:
validate_name(name, "Instance argument name")
if kind == "a":
self.attrs[name] = value
elif kind == "p":
Expand All @@ -331,6 +333,7 @@ def __init__(self, type, *args, src_loc=None, src_loc_at=0, **kwargs):
.format((kind, name, value)))

for kw, arg in kwargs.items():
validate_name(kw, "Instance keyword argument name")
if kw.startswith("a_"):
self.attrs[kw[2:]] = arg
elif kw.startswith("p_"):
Expand Down
11 changes: 11 additions & 0 deletions tests/test_hdl_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,17 @@ def test_construct(self):
("io6", (io6, "io")),
]))

def test_wrong_name(self):
with self.assertRaisesRegex(TypeError,
r"^Instance type must be a string, not 1$"):
Instance(1)
with self.assertRaisesRegex(TypeError,
r"^Instance argument name must be a string, not 1$"):
Instance("foo", ("a", 1, 2))
with self.assertRaisesRegex(NameError,
r"^Instance keyword argument name ' ' contains whitespace/control character ' '$"):
Instance("foo", **{" ": 2})

def test_cast_ports(self):
inst = Instance("foo",
("i", "s1", 1),
Expand Down

0 comments on commit 1cab145

Please sign in to comment.