Skip to content

Commit 4303d3a

Browse files
committed
Change Node.parameters to return dict of elaborated parameter values rather than internal Parameter objects
1 parent 3bb7499 commit 4303d3a

File tree

5 files changed

+31
-14
lines changed

5 files changed

+31
-14
lines changed

src/systemrdl/node.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import re
22
import itertools
33
from copy import deepcopy, copy
4-
from collections import deque
4+
from collections import deque, OrderedDict
55
from typing import TYPE_CHECKING, Optional, Iterator, Any, List, Tuple, Dict
66
from typing import Sequence, Union, overload, TypeVar, Type
77

@@ -14,8 +14,6 @@
1414
if TYPE_CHECKING:
1515
from .compiler import RDLEnvironment
1616
from .source_ref import SourceRefBase
17-
from .core.parameter import Parameter
18-
from collections import OrderedDict
1917
from markdown import Markdown
2018

2119
T = TypeVar("T")
@@ -857,14 +855,19 @@ def property_src_ref(self) -> Dict[str, 'SourceRefBase']:
857855
return self.inst.property_src_ref
858856

859857
@property
860-
def parameters(self) -> 'OrderedDict[str, Parameter]':
858+
def parameters(self) -> 'OrderedDict[str, Any]':
861859
"""
862-
Parameters of this component
860+
Returns a dictionary of the parameters of this component, and their final
861+
elaborated values.
862+
863863
These are stored in the order that they were defined
864864
865865
.. versionadded:: 1.30
866866
"""
867-
return self.inst.parameters_dict
867+
param_values_dict = OrderedDict()
868+
for param_name, param in self.inst.parameters_dict.items():
869+
param_values_dict[param_name] = param.get_value()
870+
return param_values_dict
868871

869872
@property
870873
def external(self) -> bool:

test/lib/type_hint_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,8 @@ def value_is_compatible(value, hint) -> bool:
6666
else:
6767
raise RuntimeError(f"Unhandled generic {hint}: {hint.__origin__}")
6868

69+
if hint is typing.Any:
70+
return True
71+
6972
# hint is an actual class
7073
return isinstance(value, hint)

test/test_api_type_hints.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import glob
22
import os
3-
from typing import Union, List, Optional
3+
from typing import Union, List, Optional, Dict, Any
44
import sys
55

66
from typing_extensions import Literal, get_overloads, get_type_hints
@@ -10,6 +10,7 @@
1010
from lib.type_hint_utils import value_is_compatible, hint_is
1111
from systemrdl.node import AddressableNode, FieldNode, MemNode, Node, AddrmapNode, RegNode, RegfileNode, RootNode, SignalNode, VectorNode
1212
from systemrdl import component as comp
13+
from systemrdl.source_ref import SourceRefBase
1314
from unittest_utils import RDLSourceTestCase
1415

1516
from systemrdl.walker import RDLListener, RDLSimpleWalker
@@ -114,19 +115,25 @@ def enter_Component(self, node: Node) -> None:
114115
self.test_class.assert_attr_type_hint(node, "inst_name", str)
115116
self.test_class.assert_attr_type_hint(node, "type_name", Optional[str])
116117
self.test_class.assert_attr_type_hint(node, "orig_type_name", Optional[str])
118+
self.test_class.assert_attr_type_hint(node, "inst_src_ref", Optional[SourceRefBase])
119+
self.test_class.assert_attr_type_hint(node, "def_src_ref", Optional[SourceRefBase])
120+
self.test_class.assert_attr_type_hint(node, "property_src_ref", Dict[str, SourceRefBase])
121+
self.test_class.assert_attr_type_hint(node, "parameters", Dict[str, Any])
117122
self.test_class.assert_attr_type_hint(node, "external", bool)
123+
self.test_class.assert_attr_type_hint(node, "cpuif_reset", Optional[SignalNode])
118124

119125
def enter_AddressableComponent(self, node: AddressableNode) -> None:
120126
self.test_class.assert_attr_type_hint(node, "raw_address_offset", int)
121127
self.test_class.assert_attr_type_hint(node, "raw_absolute_address", int)
122128
self.test_class.assert_attr_type_hint(node, "size", int)
123129
self.test_class.assert_attr_type_hint(node, "total_size", int)
124-
if node.is_array:
130+
if node.is_array is True:
125131
self.test_class.assert_attr_type_hint(node, "array_dimensions", List[int])
126132
self.test_class.assert_attr_type_hint(node, "array_stride", int)
127133
else:
128134
self.test_class.assertIsNone(node.array_dimensions)
129135
self.test_class.assertIsNone(node.array_stride)
136+
self.test_class.assert_attr_type_hint(node, "n_elements", int)
130137

131138

132139
def enter_VectorComponent(self, node: VectorNode) -> None:
@@ -159,6 +166,7 @@ def enter_Reg(self, node: RegNode) -> None:
159166
self.test_class.assert_attr_type_hint(node, "parent", Union[AddrmapNode, RegfileNode, MemNode])
160167
self.test_class.assert_attr_type_hint(node, "inst", comp.Reg)
161168
self.test_class.assert_attr_type_hint(node, "size", int)
169+
self.test_class.assert_attr_type_hint(node, "is_msb0_order", bool)
162170
self.test_class.assert_attr_type_hint(node, "is_virtual", bool)
163171
self.test_class.assert_attr_type_hint(node, "has_sw_writable", bool)
164172
self.test_class.assert_attr_type_hint(node, "has_sw_readable", bool)

test/test_multiple_elab.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ def test_multi_elab_params(self):
4848
rdlc = RDLCompiler(message_printer=TestPrinter())
4949
rdlc.compile_file(os.path.join(this_dir, "rdl_src/elab_params.rdl"))
5050

51-
print("1")
5251
default_root = rdlc.elaborate("elab_params")
53-
print("2")
5452
W10_root = rdlc.elaborate("elab_params", parameters={
5553
"STR": "ovr1",
5654
"INT": 10,
@@ -61,7 +59,6 @@ def test_multi_elab_params(self):
6159
self.assertEqual(default_root.top.find_by_path("r1.f").width, 1)
6260
self.assertEqual(W10_root.top.find_by_path("r1.f").width, 10)
6361

64-
# Test old parameter API - Was not explicitly made a private API!
6562
expected = {
6663
"STR": "default",
6764
"INT": 1,
@@ -70,8 +67,13 @@ def test_multi_elab_params(self):
7067
"BOOL": True,
7168
"UNUSED_STR": "asdf",
7269
}
70+
# Test old parameter API - Was not explicitly made a private API!
7371
for param in default_root.top.inst.parameters:
7472
self.assertEqual(expected[param.name], param.get_value())
73+
# Test new parameter API
74+
for name, value in default_root.top.parameters.items():
75+
self.assertEqual(expected[name], value)
76+
7577

7678
expected = {
7779
"STR": "ovr1",
@@ -81,8 +83,12 @@ def test_multi_elab_params(self):
8183
"BOOL": True,
8284
"UNUSED_STR": "ovr2",
8385
}
86+
# Test old parameter API - Was not explicitly made a private API!
8487
for param in W10_root.top.inst.parameters:
8588
self.assertEqual(expected[param.name], param.get_value())
89+
# Test new parameter API
90+
for name, value in W10_root.top.parameters.items():
91+
self.assertEqual(expected[name], value)
8692

8793

8894
def test_multi_elab_common_dpa(self):

test/test_parameters.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,15 +284,12 @@ def checkme(r, X):
284284
self.assertEqual(r.get_property("udp4"), int("0x" + "A" * X, 0))
285285
self.assertEqual(r.get_property("udp5"), 0xFFFFFFFF & ((1 << X) - 1))
286286

287-
print("r1")
288287
r1 = root.find_by_path("top.r1")
289288
checkme(r1, 8)
290289

291-
print("r2")
292290
r2 = root.find_by_path("top.r2")
293291
checkme(r2, 4)
294292

295-
print("r3")
296293
r3 = root.find_by_path("top.r3")
297294
checkme(r3, 8)
298295

0 commit comments

Comments
 (0)