-
Notifications
You must be signed in to change notification settings - Fork 8
/
tuple.py
176 lines (151 loc) · 6.28 KB
/
tuple.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from __future__ import annotations
from typing import TYPE_CHECKING
import structlog
from puya.awst import wtypes
from puya.awst.nodes import (
ARC4Encode,
Expression,
IndexExpression,
Literal,
TupleExpression,
UInt64Constant,
)
from puya.awst_build.eb._utils import bool_eval_to_constant
from puya.awst_build.eb.arc4.base import (
ARC4ClassExpressionBuilder,
ARC4EncodedExpressionBuilder,
)
from puya.awst_build.eb.base import (
ExpressionBuilder,
GenericClassExpressionBuilder,
TypeClassExpressionBuilder,
)
from puya.awst_build.eb.tuple import TupleExpressionBuilder
from puya.awst_build.eb.var_factory import var_expression
from puya.errors import CodeError, InternalError
if TYPE_CHECKING:
from collections.abc import Sequence
import mypy.nodes
from puya.parse import SourceLocation
logger: structlog.types.FilteringBoundLogger = structlog.get_logger(__name__)
class ARC4TupleGenericClassExpressionBuilder(GenericClassExpressionBuilder):
def index_multiple(
self, indexes: Sequence[ExpressionBuilder | Literal], location: SourceLocation
) -> TypeClassExpressionBuilder:
tuple_item_types = list[wtypes.WType]()
for index in indexes:
match index:
case TypeClassExpressionBuilder() as type_class:
wtype = type_class.produces()
if not wtypes.is_arc4_encoded_type(wtype):
raise CodeError(
"ARC4 Tuples can only contain ARC4 encoded values", location
)
tuple_item_types.append(wtype)
case _:
raise CodeError("Invalid type parameter", index.source_location)
return ARC4TupleClassExpressionBuilder(
location, wtypes.ARC4Tuple.from_types(tuple_item_types)
)
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:
return tuple_constructor(args, None, location)
def tuple_constructor(
args: Sequence[ExpressionBuilder | Literal],
wtype: wtypes.ARC4Tuple | None,
location: SourceLocation,
) -> ExpressionBuilder:
match args:
case [TupleExpressionBuilder() as teb]:
tuple_ex = teb.rvalue()
if not isinstance(tuple_ex, TupleExpression):
raise CodeError("arc4.Tuple must be instantiated with a tuple", location)
if wtype is None:
wtype = wtypes.ARC4Tuple.from_types(tuple_ex.wtype.types)
else:
expected_type = wtypes.WTuple.from_types(wtype.types)
if tuple_ex.wtype != expected_type:
raise CodeError(
f"Invalid arg type: expected {expected_type}, got {tuple_ex.wtype}",
location,
)
return var_expression(
ARC4Encode(value=tuple_ex, wtype=wtype, source_location=location)
)
raise CodeError("Invalid/unhandled arguments", location)
class ARC4TupleClassExpressionBuilder(ARC4ClassExpressionBuilder):
def __init__(self, location: SourceLocation, wtype: wtypes.ARC4Tuple | None = None):
super().__init__(location)
self.wtype = wtype
def produces(self) -> wtypes.WType:
if not self.wtype:
# TODO: make CodeError
raise InternalError(
"Cannot resolve wtype of generic EB until the index method is called with the "
"generic type parameter."
)
return self.wtype
def index(
self, index: ExpressionBuilder | Literal, location: SourceLocation
) -> ExpressionBuilder:
return self.index_multiple((index,), location)
def index_multiple(
self,
indexes: Sequence[ExpressionBuilder | Literal],
location: SourceLocation,
) -> TypeClassExpressionBuilder:
tuple_item_types = list[wtypes.WType]()
for index in indexes:
match index:
case TypeClassExpressionBuilder() as type_class:
wtype = type_class.produces()
if not wtypes.is_arc4_encoded_type(wtype):
raise CodeError(
"ARC4 Tuples can only contain ARC4 encoded values", location
)
tuple_item_types.append(wtype)
case _:
raise CodeError("Invalid type parameter", index.source_location)
self.wtype = wtypes.ARC4Tuple.from_types(tuple_item_types)
return self
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:
return tuple_constructor(args, self.wtype, location)
class ARC4TupleExpressionBuilder(ARC4EncodedExpressionBuilder):
def __init__(self, expr: Expression):
assert isinstance(expr.wtype, wtypes.ARC4Tuple)
self.wtype: wtypes.ARC4Tuple = expr.wtype
super().__init__(expr)
def index(
self, index: ExpressionBuilder | Literal, location: SourceLocation
) -> ExpressionBuilder:
match index:
case Literal(value=int(index_int), source_location=index_loc):
try:
item_wtype = self.wtype.types[index_int]
except IndexError as ex:
raise CodeError("Tuple index out of bounds", index_loc) from ex
return var_expression(
IndexExpression(
source_location=location,
base=self.expr,
index=UInt64Constant(value=index_int, source_location=index_loc),
wtype=item_wtype,
)
)
case _:
raise CodeError(f"{self.wtype.name} can only be indexed with literal values")
def bool_eval(self, location: SourceLocation, *, negate: bool = False) -> ExpressionBuilder:
return bool_eval_to_constant(value=True, location=location, negate=negate)