Skip to content

Commit aeeedda

Browse files
committed
Significant cleanup of type hinting. #242
1 parent 2e18075 commit aeeedda

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+445
-284
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ dependencies = [
1010
"antlr4-python3-runtime >= 4.11, < 4.14",
1111
"colorama",
1212
"markdown",
13+
"typing_extensions",
1314
]
1415
authors = [
1516
{name="Alex Mykyta"},

src/systemrdl/ast/cast.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
if TYPE_CHECKING:
1010
from ..compiler import RDLEnvironment
1111
from ..source_ref import SourceRefBase
12-
from rdltypes.typing import PreElabRDLType
12+
from ..rdltypes.typing import PreElabRDLType
1313

1414
OptionalSourceRef = Optional[SourceRefBase]
1515

@@ -22,6 +22,7 @@ class WidthCast(ASTNode):
2222
def __init__(self, env: 'RDLEnvironment', src_ref: 'OptionalSourceRef', v: ASTNode, w_expr: Optional[ASTNode]=None, w_int: int=64):
2323
super().__init__(env, src_ref)
2424

25+
self.w_expr: Optional[ASTNode]
2526
if w_expr is not None:
2627
self.v = v
2728
self.w_expr = w_expr
@@ -33,6 +34,7 @@ def __init__(self, env: 'RDLEnvironment', src_ref: 'OptionalSourceRef', v: ASTNo
3334

3435
def predict_type(self) -> Type[int]:
3536
if self.cast_width is None:
37+
assert self.w_expr is not None # mutually exclusive
3638
if not is_castable(self.w_expr.predict_type(), int):
3739
self.msg.fatal(
3840
"Width operand of cast expression is not a compatible numeric type",
@@ -48,13 +50,15 @@ def predict_type(self) -> Type[int]:
4850

4951
def get_min_eval_width(self) -> int:
5052
if self.cast_width is None:
53+
assert self.w_expr is not None # mutually exclusive
5154
self.cast_width = int(self.w_expr.get_value())
5255
return self.cast_width
5356

5457

5558
def get_value(self, eval_width: Optional[int]=None) -> int:
5659
# Truncate to cast width instead of eval width
5760
if self.cast_width is None:
61+
assert self.w_expr is not None # mutually exclusive
5862
self.cast_width = int(self.w_expr.get_value())
5963
if self.cast_width == 0:
6064
self.msg.fatal(

src/systemrdl/ast/conditional.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
if TYPE_CHECKING:
88
from ..compiler import RDLEnvironment
99
from ..source_ref import SourceRefBase
10-
from rdltypes.typing import PreElabRDLType
10+
from ..rdltypes.typing import PreElabRDLType
1111

1212
OptionalSourceRef = Optional[SourceRefBase]
1313

@@ -21,7 +21,7 @@ def __init__(self, env: 'RDLEnvironment', src_ref: 'OptionalSourceRef', i: ASTNo
2121
self.i = i
2222
self.j = j
2323
self.k = k
24-
self.is_numeric = None # type: bool
24+
self.is_numeric: bool = False
2525

2626
def predict_type(self) -> 'PreElabRDLType':
2727
t_i = self.i.predict_type()
@@ -35,7 +35,7 @@ def predict_type(self) -> 'PreElabRDLType':
3535
t_j = self.j.predict_type()
3636
t_k = self.k.predict_type()
3737

38-
typ = None # type: Any
38+
typ: Any = None
3939
if is_castable(t_j, int) and is_castable(t_k, int):
4040
self.is_numeric = True
4141
typ = int

src/systemrdl/ast/literals.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
if TYPE_CHECKING:
1111
from ..compiler import RDLEnvironment
1212
from ..source_ref import SourceRefBase
13-
from rdltypes.typing import PreElabRDLType, RDLValue
13+
from ..rdltypes.typing import PreElabRDLType, RDLValue
1414

1515
OptionalSourceRef = Optional[SourceRefBase]
1616

src/systemrdl/ast/references.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from ..compiler import RDLEnvironment
1212
from ..core.parameter import Parameter
1313
from ..source_ref import SourceRefBase
14-
from rdltypes.typing import PreElabRDLType, RDLValue
14+
from ..rdltypes.typing import PreElabRDLType
1515

1616
OptionalSourceRef = Optional[SourceRefBase]
1717

@@ -59,7 +59,12 @@ def predict_type(self) -> 'PreElabRDLType':
5959
"Cannot index non-array type",
6060
self.array.src_ref
6161
)
62-
assert isinstance(array_type, rdltypes.ArrayedType)
62+
if array_type.element_type is None:
63+
# Array type is not known, therefore it must be an emptyy array.
64+
self.msg.fatal(
65+
"Array is empty. Cannot index",
66+
self.array.src_ref
67+
)
6368

6469
return array_type.element_type
6570

@@ -162,7 +167,8 @@ def predict_type(self) -> Type[comp.Component]:
162167
referenced.
163168
Also do some checks on the array indexes
164169
"""
165-
current_comp = self.ref_root
170+
current_comp: Optional[comp.Component] = self.ref_root
171+
assert current_comp is not None
166172
for name, array_suffixes, name_src_ref in self.ref_elements:
167173

168174
# find instance
@@ -179,8 +185,7 @@ def predict_type(self) -> Type[comp.Component]:
179185
array_suffix.predict_type()
180186

181187
# Check array suffixes
182-
if (isinstance(current_comp, comp.AddressableComponent)) and current_comp.is_array:
183-
assert isinstance(current_comp.array_dimensions, list)
188+
if (isinstance(current_comp, comp.AddressableComponent)) and current_comp.array_dimensions:
184189
# is an array
185190
if len(array_suffixes) != len(current_comp.array_dimensions):
186191
self.msg.fatal(

src/systemrdl/ast/relational.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def __init__(self, env: 'RDLEnvironment', src_ref: 'OptionalSourceRef', l: ASTNo
2121
super().__init__(env, src_ref)
2222
self.l = l
2323
self.r = r
24-
self.is_numeric = None # type: bool
24+
self.is_numeric: bool = False
2525

2626
def predict_type(self) -> Type[bool]:
2727
l_type = self.l.predict_type()

src/systemrdl/ast/sequence.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Concatenate(ASTNode):
1313
def __init__(self, env: 'RDLEnvironment', src_ref: 'OptionalSourceRef', elements: List[ASTNode]):
1414
super().__init__(env, src_ref)
1515
self.elements = elements
16-
self.type = None # type: Union[Type[int], Type[str]]
16+
self.type: Union[Type[int], Type[str]] = int
1717

1818
def predict_type(self) -> Union[Type[int], Type[str]]:
1919
element_iter = iter(self.elements)
@@ -73,11 +73,10 @@ def __init__(self, env: 'RDLEnvironment', src_ref: 'OptionalSourceRef', reps: AS
7373
super().__init__(env, src_ref)
7474
self.reps = reps
7575
self.concat = concat
76-
self.type = None # type: Union[Type[int], Type[str]]
77-
self.reps_value = None # type: int
76+
self.type: Union[Type[int], Type[str]] = int
77+
self.reps_value: Optional[int] = None
7878

79-
def predict_type(self):
80-
# type: () -> Union[Type[int], Type[str]]
79+
def predict_type(self) -> Union[Type[int], Type[str]]:
8180

8281
if not is_castable(self.reps.predict_type(), int):
8382
self.msg.fatal(

src/systemrdl/compiler.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ def included_files(self) -> Iterable[str]:
4646

4747

4848
class RDLCompiler:
49-
5049
def __init__(self, **kwargs: Any):
5150
"""
5251
RDLCompiler constructor.
@@ -110,14 +109,14 @@ def __init__(self, **kwargs: Any):
110109
#: This will be deprecated in a future release. See this page for more details: https://github.com/SystemRDL/systemrdl-compiler/issues/168
111110
self.msg = self.env.msg
112111

113-
self.namespace = NamespaceRegistry(self.env) # type: NamespaceRegistry
114-
self.visitor = RootVisitor(self)
115-
self.root = self.visitor.component # type: comp.Root # type: ignore
112+
self.namespace: NamespaceRegistry = NamespaceRegistry(self.env)
113+
self.visitor: RootVisitor = RootVisitor(self)
114+
self.root = self.visitor.component
116115

117116

118117
def define_udp(
119118
self, name: str, valid_type: Any,
120-
valid_components: 'Optional[Set[Type[comp.Component]]]'=None,
119+
valid_components: Optional[Set[Type[comp.Component]]]=None,
121120
default: Any=None
122121
) -> None:
123122

@@ -130,6 +129,15 @@ def define_udp(
130129
raise ValueError(f"UDP definition's name '{name}' conflicts with existing built-in RDL property")
131130
if name in self.env.property_rules.user_properties:
132131
raise ValueError(f"UDP '{name}' has already been defined")
132+
if valid_components is None:
133+
valid_components = {
134+
comp.Field,
135+
comp.Reg,
136+
comp.Regfile,
137+
comp.Addrmap,
138+
comp.Mem,
139+
comp.Signal,
140+
}
133141

134142
udp = LegacyExternalUserProperty(
135143
self.env,
@@ -386,8 +394,10 @@ def elaborate(self, top_def_name: Optional[str]=None, inst_name: Optional[str]=N
386394

387395
literal_expr = ast.ExternalLiteral(self.env, value)
388396
assign_expr = ast.AssignmentCast(self.env, None, literal_expr, parameter.param_type)
389-
assign_type = assign_expr.predict_type()
390-
if assign_type is None:
397+
try:
398+
# Try to predict type to test if ExternalLiteral maps to a valid RDL type
399+
assign_expr.predict_type()
400+
except ValueError:
391401
self.msg.fatal(f"Incorrect type for top-level parameter override of '{param_name}'")
392402

393403
parameter.expr = assign_expr

0 commit comments

Comments
 (0)