-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcompiled.py
157 lines (138 loc) · 5.85 KB
/
compiled.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
import typing
from collections.abc import Mapping, Sequence
import mypy.nodes
from puya.awst.nodes import CompiledContract, CompiledLogicSig, Expression
from puya.awst.txn_fields import TxnField
from puya.log import get_logger
from puya.models import LogicSigReference
from puya.parse import SourceLocation
from puyapy.awst_build import pytypes
from puyapy.awst_build.eb import _expect as expect
from puyapy.awst_build.eb._base import FunctionBuilder
from puyapy.awst_build.eb._utils import dummy_value
from puyapy.awst_build.eb.dict_ import DictLiteralBuilder
from puyapy.awst_build.eb.interface import InstanceBuilder, NodeBuilder
from puyapy.awst_build.eb.logicsig import LogicSigExpressionBuilder
from puyapy.awst_build.eb.tuple import TupleExpressionBuilder
from puyapy.awst_build.utils import get_arg_mapping
logger = get_logger(__name__)
# these names should match pytypes CompiledContract definition
PROGRAM_FIELDS = {
"approval_program": TxnField.ApprovalProgramPages,
"clear_state_program": TxnField.ClearStateProgramPages,
}
APP_ALLOCATION_FIELDS = {
"extra_program_pages": TxnField.ExtraProgramPages,
"global_bytes": TxnField.GlobalNumByteSlice,
"global_uints": TxnField.GlobalNumUint,
"local_bytes": TxnField.LocalNumByteSlice,
"local_uints": TxnField.LocalNumUint,
}
class CompiledContractExpressionBuilder(TupleExpressionBuilder):
def __init__(self, expr: Expression) -> None:
super().__init__(expr, pytypes.CompiledContractType)
class CompiledLogicSigExpressionBuilder(TupleExpressionBuilder):
def __init__(self, expr: Expression) -> None:
super().__init__(expr, pytypes.CompiledLogicSigType)
_TEMPLATE_VAR_KWARG_NAMES = [
"template_vars",
"template_vars_prefix",
]
class CompileContractFunctionBuilder(FunctionBuilder):
@typing.override
def call(
self,
args: Sequence[NodeBuilder],
arg_kinds: list[mypy.nodes.ArgKind],
arg_names: list[str | None],
location: SourceLocation,
) -> InstanceBuilder:
contract_arg_name = "contract"
arg_map, _ = get_arg_mapping(
args=args,
arg_names=arg_names,
call_location=location,
raise_on_missing=False,
required_positional_names=[contract_arg_name],
optional_kw_only=[
*APP_ALLOCATION_FIELDS,
*_TEMPLATE_VAR_KWARG_NAMES,
],
)
prefix, template_vars = _extract_prefix_template_args(arg_map)
allocation_overrides = {}
for python_name, field in APP_ALLOCATION_FIELDS.items():
if arg := arg_map.get(python_name):
allocation_overrides[field] = expect.argument_of_type_else_dummy(
arg, pytypes.UInt64Type, resolve_literal=True
).resolve()
result_type = pytypes.CompiledContractType
match arg_map[contract_arg_name]:
case NodeBuilder(pytype=pytypes.TypeType(typ=pytypes.ContractType() as contract_typ)):
contract = contract_typ.name
case invalid_or_none:
if invalid_or_none is None:
# if None (=missing), then error message already logged by get_arg_mapping
return dummy_value(result_type, location)
return expect.not_this_type(
invalid_or_none, default=expect.default_dummy_value(result_type)
)
return CompiledContractExpressionBuilder(
CompiledContract(
contract=contract,
allocation_overrides=allocation_overrides,
prefix=prefix,
template_variables=template_vars,
wtype=result_type.wtype,
source_location=location,
)
)
class CompileLogicSigFunctionBuilder(FunctionBuilder):
@typing.override
def call(
self,
args: Sequence[NodeBuilder],
arg_kinds: list[mypy.nodes.ArgKind],
arg_names: list[str | None],
location: SourceLocation,
) -> InstanceBuilder:
logicsig_arg_name = "logicsig"
arg_map, _ = get_arg_mapping(
args=args,
arg_names=arg_names,
call_location=location,
raise_on_missing=False,
required_positional_names=[logicsig_arg_name],
optional_kw_only=_TEMPLATE_VAR_KWARG_NAMES,
)
match arg_map.get(logicsig_arg_name):
case LogicSigExpressionBuilder(ref=logic_sig):
pass
case missing_or_invalid:
logic_sig = LogicSigReference("") # dummy reference
# if None (=missing), then error message already logged by get_arg_mapping
if missing_or_invalid is not None:
expect.not_this_type(missing_or_invalid, default=expect.default_none)
prefix, template_vars = _extract_prefix_template_args(arg_map)
return CompiledLogicSigExpressionBuilder(
CompiledLogicSig(
logic_sig=logic_sig,
prefix=prefix,
template_variables=template_vars,
wtype=pytypes.CompiledLogicSigType.wtype,
source_location=location,
)
)
def _extract_prefix_template_args(
name_args: dict[str, NodeBuilder],
) -> tuple[str | None, Mapping[str, Expression]]:
prefix: str | None = None
template_vars: Mapping[str, Expression] = {}
if template_vars_node := name_args.get("template_vars"):
if isinstance(template_vars_node, DictLiteralBuilder):
template_vars = {k: v.resolve() for k, v in template_vars_node.mapping.items()}
else:
expect.not_this_type(template_vars_node, default=expect.default_none)
if prefix_node := name_args.get("template_vars_prefix"):
prefix = expect.simple_string_literal(prefix_node, default=expect.default_none)
return prefix, template_vars