-
Notifications
You must be signed in to change notification settings - Fork 8
/
group.py
156 lines (138 loc) · 5.25 KB
/
group.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from __future__ import annotations
import abc
import typing
from puya.awst import wtypes
from puya.awst.nodes import (
CheckedMaybe,
Expression,
IntrinsicCall,
Literal,
NumericComparison,
NumericComparisonExpression,
ReinterpretCast,
SingleEvaluation,
TxnField,
UInt64Constant,
)
from puya.awst_build.eb.base import (
ExpressionBuilder,
IntermediateExpressionBuilder,
TypeClassExpressionBuilder,
)
from puya.awst_build.eb.transaction.base import (
BaseTransactionExpressionBuilder,
expect_wtype,
)
from puya.awst_build.eb.var_factory import var_expression
from puya.awst_build.utils import expect_operand_wtype
from puya.errors import CodeError
if typing.TYPE_CHECKING:
from collections.abc import Sequence
import mypy.nodes
from puya.parse import SourceLocation
class GroupTransactionExpressionBuilder(BaseTransactionExpressionBuilder):
def __init__(self, expr: Expression):
self.wtype = expect_wtype(expr, wtypes.WGroupTransaction)
super().__init__(expr)
def get_field_value(self, field: TxnField, location: SourceLocation) -> Expression:
return IntrinsicCall(
source_location=location,
wtype=field.wtype,
op_code="gtxns",
immediates=[field.immediate],
stack_args=[self.expr],
)
def get_array_member(self, field: TxnField, location: SourceLocation) -> ExpressionBuilder:
return GroupTransactionArrayExpressionBuilder(self.expr, field, location)
class GroupTransactionArrayExpressionBuilder(IntermediateExpressionBuilder):
def __init__(
self,
transaction: Expression,
field: TxnField,
location: SourceLocation,
):
super().__init__(location)
self.transaction = transaction
self.field = field
def call(
self,
args: Sequence[ExpressionBuilder | Literal],
arg_kinds: list[mypy.nodes.ArgKind],
arg_names: list[str | None],
location: SourceLocation,
original_expr: mypy.nodes.CallExpr,
) -> ExpressionBuilder:
match args:
case [(ExpressionBuilder() | Literal(value=int())) as eb]:
index_expr = expect_operand_wtype(eb, wtypes.uint64_wtype)
expr = IntrinsicCall(
source_location=location,
wtype=self.field.wtype,
op_code="gtxnsas",
immediates=[self.field.immediate],
stack_args=[self.transaction, index_expr],
)
return var_expression(expr)
case _:
raise CodeError("Invalid/unhandled arguments", location)
def check_transaction_type(
transaction_index: Expression,
expected_transaction_type: wtypes.WGroupTransaction,
location: SourceLocation,
) -> Expression:
if expected_transaction_type.transaction_type is None:
return transaction_index
transaction_index_tmp = SingleEvaluation(transaction_index)
return ReinterpretCast(
source_location=location,
wtype=expected_transaction_type,
expr=CheckedMaybe.from_tuple_items(
transaction_index_tmp,
NumericComparisonExpression(
lhs=IntrinsicCall(
op_code="gtxns",
immediates=["TypeEnum"],
stack_args=[transaction_index_tmp],
wtype=wtypes.uint64_wtype,
source_location=location,
),
operator=NumericComparison.eq,
rhs=UInt64Constant(
value=expected_transaction_type.transaction_type.value,
teal_alias=expected_transaction_type.transaction_type.name,
source_location=location,
),
source_location=location,
),
location,
comment=f"transaction type is {expected_transaction_type.transaction_type.name}",
),
)
class GroupTransactionClassExpressionBuilder(TypeClassExpressionBuilder, abc.ABC):
def __init__(self, location: SourceLocation, wtype: wtypes.WGroupTransaction):
super().__init__(location)
self.wtype = wtype
def produces(self) -> wtypes.WType:
return self.wtype
def call(
self,
args: typing.Sequence[ExpressionBuilder | Literal],
arg_kinds: list[mypy.nodes.ArgKind],
arg_names: list[str | None],
location: SourceLocation,
original_expr: mypy.nodes.CallExpr,
) -> ExpressionBuilder:
match args:
case [ExpressionBuilder() as eb]:
group_index = expect_operand_wtype(eb, wtypes.uint64_wtype)
case [Literal(value=int(int_value), source_location=loc)]:
group_index = UInt64Constant(value=int_value, source_location=loc)
case _:
raise CodeError("Invalid/unhandled arguments", location)
wtype = self.wtype
txn = (
check_transaction_type(group_index, wtype, location)
if isinstance(wtype, wtypes.WGroupTransaction) and wtype.transaction_type is not None
else ReinterpretCast(expr=group_index, wtype=wtype, source_location=location)
)
return GroupTransactionExpressionBuilder(txn)