From 118fc3d18dd6e8a9f8f242564d9b1d1c7e0368ea Mon Sep 17 00:00:00 2001 From: Marek Materzok Date: Mon, 11 Mar 2024 12:01:46 +0100 Subject: [PATCH] Remove records from assign --- test/transactions/test_assign.py | 16 +++++++-------- transactron/utils/_typing.py | 7 +------ transactron/utils/assign.py | 34 ++++++++++---------------------- 3 files changed, 19 insertions(+), 38 deletions(-) diff --git a/test/transactions/test_assign.py b/test/transactions/test_assign.py index 47f72800b..73d5b28f9 100644 --- a/test/transactions/test_assign.py +++ b/test/transactions/test_assign.py @@ -3,7 +3,7 @@ from amaranth.lib import data from amaranth.hdl._ast import ArrayProxy, Slice -from transactron.utils._typing import LayoutLike +from transactron.utils._typing import MethodLayout from transactron.utils import AssignType, assign from transactron.utils.assign import AssignArg, AssignFields @@ -54,15 +54,15 @@ def mkstruct(layout): ], ) class TestAssign(TestCase): - # constructs `assign` arguments (records, proxies, dicts) which have an "inner" and "outer" part - # parameterized with a Record-like constructor and a layout of the inner part - build: Callable[[Callable[[LayoutLike], AssignArg], LayoutLike], AssignArg] + # constructs `assign` arguments (views, proxies, dicts) which have an "inner" and "outer" part + # parameterized with a constructor and a layout of the inner part + build: Callable[[Callable[[MethodLayout], AssignArg], MethodLayout], AssignArg] # constructs field specifications for `assign`, takes field specifications for the inner part wrap: Callable[[AssignFields], AssignFields] # extracts the inner part of the structure - extr: Callable[[AssignArg], Record | ArrayProxy] - # Record-like constructor, takes a record layout - mk: Callable[[LayoutLike], AssignArg] + extr: Callable[[AssignArg], ArrayProxy] + # constructor, takes a layout + mk: Callable[[MethodLayout], AssignArg] def test_rhs_exception(self): with self.assertRaises(KeyError): @@ -99,7 +99,7 @@ def test_wrong_bits(self): ("list", layout_ab, layout_ab, ["a", "a"]), ] ) - def test_assign_a(self, name, layout1: LayoutLike, layout2: LayoutLike, atype: AssignType): + def test_assign_a(self, name, layout1: MethodLayout, layout2: MethodLayout, atype: AssignType): lhs = self.build(self.mk, layout1) rhs = self.build(self.mk, layout2) alist = list(assign(lhs, rhs, fields=self.wrap(atype))) diff --git a/transactron/utils/_typing.py b/transactron/utils/_typing.py index a44dc6610..5acd3ab70 100644 --- a/transactron/utils/_typing.py +++ b/transactron/utils/_typing.py @@ -14,14 +14,13 @@ Any, TYPE_CHECKING, ) -from collections.abc import Iterable, Iterator, Mapping, Sequence +from collections.abc import Iterable, Iterator, Mapping from contextlib import AbstractContextManager from enum import Enum from amaranth import * from amaranth.lib.data import StructLayout, View from amaranth.lib.wiring import Flow, Member from amaranth.hdl import ShapeCastable, ValueCastable -from amaranth.hdl.rec import Direction, Layout if TYPE_CHECKING: from amaranth.hdl._ast import Statement @@ -33,7 +32,6 @@ "ValueLike", "ShapeLike", "StatementLike", - "LayoutLike", "SwitchKey", "SrcLoc", "MethodLayout", @@ -60,9 +58,6 @@ ValueLike: TypeAlias = Value | int | Enum | ValueCastable ShapeLike: TypeAlias = Shape | ShapeCastable | int | range | type[Enum] StatementLike: TypeAlias = Union["Statement", Iterable["StatementLike"]] -LayoutLike: TypeAlias = ( - Layout | Sequence[tuple[str, "ShapeLike | LayoutLike"] | tuple[str, "ShapeLike | LayoutLike", Direction]] -) SwitchKey: TypeAlias = str | int | Enum SrcLoc: TypeAlias = tuple[str, int] diff --git a/transactron/utils/assign.py b/transactron/utils/assign.py index b3bee191e..0be471e80 100644 --- a/transactron/utils/assign.py +++ b/transactron/utils/assign.py @@ -41,8 +41,6 @@ def flatten_elems(proxy: ArrayProxy): def assign_arg_fields(val: AssignArg) -> Optional[set[str]]: if isinstance(val, ArrayProxy): return arrayproxy_fields(val) - elif isinstance(val, Record): - return set(val.fields) elif isinstance(val, data.View): layout = val.shape() if isinstance(layout, data.StructLayout): @@ -57,7 +55,7 @@ def assign( """Safe structured assignment. This function recursively generates assignment statements for - field-containing structures. This includes: Amaranth `Record`\\s, + field-containing structures. This includes: Amaranth `View`\\s using `StructLayout`, Python `dict`\\s. In case of mismatching fields or bit widths, error is raised. @@ -68,16 +66,16 @@ def assign( The bit width check is performed if: - - Any of `lhs` or `rhs` is a `Record` or `View`. + - Any of `lhs` or `rhs` is a `View`. - Both `lhs` and `rhs` have an explicitly defined shape (e.g. are a - `Signal`, a field of a `Record` or a `View`). + `Signal`, a field of a `View`). Parameters ---------- - lhs : Record or View or Value-castable or dict - Record, signal or dict being assigned. - rhs : Record or View or Value-castable or dict - Record, signal or dict containing assigned values. + lhs : View or Value-castable or dict + View, signal or dict being assigned. + rhs : View or Value-castable or dict + View, signal or dict containing assigned values. fields : AssignType or Iterable or Mapping, optional Determines which fields will be assigned. Possible values: @@ -109,18 +107,8 @@ def assign( if lhs_fields is not None and rhs_fields is not None: # asserts for type checking - assert ( - isinstance(lhs, Record) - or isinstance(lhs, ArrayProxy) - or isinstance(lhs, Mapping) - or isinstance(lhs, data.View) - ) - assert ( - isinstance(rhs, Record) - or isinstance(rhs, ArrayProxy) - or isinstance(rhs, Mapping) - or isinstance(rhs, data.View) - ) + assert isinstance(lhs, ArrayProxy) or isinstance(lhs, Mapping) or isinstance(lhs, data.View) + assert isinstance(rhs, ArrayProxy) or isinstance(rhs, Mapping) or isinstance(rhs, data.View) if fields is AssignType.COMMON: names = lhs_fields & rhs_fields @@ -166,9 +154,7 @@ def has_explicit_shape(val: ValueLike): return isinstance(val, Signal) or isinstance(val, ArrayProxy) if ( - isinstance(lhs, Record) - or isinstance(rhs, Record) - or isinstance(lhs, data.View) + isinstance(lhs, data.View) or isinstance(rhs, data.View) or (lhs_strict or has_explicit_shape(lhs)) and (rhs_strict or has_explicit_shape(rhs))