Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change TransactionBase into a Protocol #472

Merged
merged 13 commits into from
Oct 20, 2023
27 changes: 21 additions & 6 deletions transactron/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@
from collections.abc import Sequence, Iterable, Callable, Mapping, Iterator
from contextlib import contextmanager
from enum import Enum, auto
from typing import ClassVar, NoReturn, TypeAlias, TypedDict, Union, Optional, Tuple
from typing import (
ClassVar,
NoReturn,
TypeAlias,
TypedDict,
Union,
Optional,
Tuple,
TypeVar,
Protocol,
runtime_checkable,
)
from graphlib import TopologicalSorter
from typing_extensions import Self
from amaranth import *
Expand Down Expand Up @@ -38,6 +49,7 @@
TransactionScheduler: TypeAlias = Callable[["MethodMap", TransactionGraph, TransactionGraphCC, PriorityOrder], Module]
RecordDict: TypeAlias = ValueLike | Mapping[str, "RecordDict"]
TransactionOrMethod: TypeAlias = Union["Transaction", "Method"]
TransactionOrMethodBound = TypeVar("TransactionOrMethodBound", "Transaction", "Method")


class Priority(Enum):
Expand Down Expand Up @@ -670,15 +682,20 @@ def elaborate(self, platform):
return self.main_module


class TransactionBase(Owned):
@runtime_checkable
tilk marked this conversation as resolved.
Show resolved Hide resolved
class TransactionBase(Owned, Protocol):
stack: ClassVar[list[Union["Transaction", "Method"]]] = []
def_counter: ClassVar[count] = count()
def_order: int
defined: bool = False
Kristopher38 marked this conversation as resolved.
Show resolved Hide resolved
name: str
method_uses: dict["Method", Tuple[ValueLike, ValueLike]]
relations: list[RelationBase]
simultaneous_list: list[TransactionOrMethod]
independent_list: list[TransactionOrMethod]
Kristopher38 marked this conversation as resolved.
Show resolved Hide resolved

def __init__(self):
self.method_uses: dict[Method, Tuple[ValueLike, ValueLike]] = dict()
self.method_uses: dict["Method", Tuple[ValueLike, ValueLike]] = dict()
self.relations: list[RelationBase] = []
self.simultaneous_list: list[TransactionOrMethod] = []
self.independent_list: list[TransactionOrMethod] = []
Expand Down Expand Up @@ -769,9 +786,7 @@ def _independent(self, *others: TransactionOrMethod) -> None:
self.independent_list += others

@contextmanager
def context(self, m: TModule) -> Iterator[Self]:
assert isinstance(self, Transaction) or isinstance(self, Method) # for typing

def context(self: TransactionOrMethodBound, m: TModule) -> Iterator[TransactionOrMethodBound]:
tilk marked this conversation as resolved.
Show resolved Hide resolved
parent = TransactionBase.peek()
if parent is not None:
parent.schedule_before(self)
Expand Down
5 changes: 2 additions & 3 deletions transactron/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
"""

from enum import IntFlag
from abc import ABC
from collections import defaultdict
from typing import Literal, Optional
from typing import Literal, Optional, Protocol

from amaranth.hdl.ir import Elaboratable, Fragment
from .tracing import TracingFragment


class Owned(ABC):
class Owned(Protocol):
name: str
owner: Optional[Elaboratable]

Expand Down