-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsubroutine.py
1244 lines (1155 loc) · 52.8 KB
/
subroutine.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import typing
from collections.abc import Callable, Sequence
import attrs
import mypy.nodes
import mypy.patterns
import mypy.types
from puya import log
from puya.awst.nodes import (
AppStateExpression,
AppStorageDefinition,
AssertExpression,
AssignmentExpression,
AssignmentStatement,
BinaryBooleanOperator,
Block,
BooleanBinaryOperation,
BytesConstant,
BytesEncoding,
ConditionalExpression,
ContractMethod,
Expression,
ExpressionStatement,
ForInLoop,
Goto,
IfElse,
InstanceSuperMethodTarget,
Label,
LoopContinue,
LoopExit,
Lvalue,
ReturnStatement,
Statement,
Subroutine,
SubroutineArgument,
SubroutineID,
Switch,
VarExpression,
WhileLoop,
)
from puya.errors import CodeError, InternalError
from puya.models import ARC4MethodConfig, ContractReference, LogicSigReference
from puya.parse import SourceLocation
from puyapy.awst_build import constants, pytypes
from puyapy.awst_build.base_mypy_visitor import BaseMyPyVisitor
from puyapy.awst_build.context import ASTConversionModuleContext
from puyapy.awst_build.eb import _expect as expect
from puyapy.awst_build.eb._literals import LiteralBuilderImpl
from puyapy.awst_build.eb.arc4 import ARC4ClientTypeBuilder
from puyapy.awst_build.eb.conditional_literal import ConditionalLiteralBuilder
from puyapy.awst_build.eb.contracts import (
ContractSelfExpressionBuilder,
ContractTypeExpressionBuilder,
)
from puyapy.awst_build.eb.dict_ import DictLiteralBuilder
from puyapy.awst_build.eb.factories import (
builder_for_instance,
builder_for_type,
try_get_builder_for_func,
)
from puyapy.awst_build.eb.interface import (
BuilderBinaryOp,
BuilderComparisonOp,
BuilderUnaryOp,
CallableBuilder,
InstanceBuilder,
LiteralBuilder,
NodeBuilder,
StorageProxyConstructorResult,
)
from puyapy.awst_build.eb.logicsig import LogicSigExpressionBuilder
from puyapy.awst_build.eb.none import NoneTypeBuilder
from puyapy.awst_build.eb.subroutine import SubroutineInvokerExpressionBuilder
from puyapy.awst_build.utils import (
extract_bytes_literal_from_mypy,
get_unaliased_fullname,
maybe_resolve_literal,
require_callable_type,
)
from puyapy.models import ContractFragmentBase
from puyapy.parse import parse_docstring
logger = log.get_logger(__name__)
@attrs.frozen
class ContractMethodInfo:
fragment: ContractFragmentBase
contract_type: pytypes.ContractType
is_abstract: bool
arc4_method_config: ARC4MethodConfig | None
class FunctionASTConverter(BaseMyPyVisitor[Statement | Sequence[Statement] | None, NodeBuilder]):
def __init__(
self,
context: ASTConversionModuleContext,
func_def: mypy.nodes.FuncDef,
contract_method_info: ContractMethodInfo | None,
source_location: SourceLocation,
):
super().__init__(context=context)
func_loc = self._location(func_def) # TODO: why not source_location ??
self.contract_method_info = contract_method_info
self.func_def = func_def
self._precondition(
not func_def.is_trivial_body,
"trivial functions should be skipped at a higher level",
func_loc,
)
type_info = func_def.type
self._precondition(
isinstance(type_info, mypy.types.CallableType),
"should only receive functions/methods with a type of CallableType",
func_loc,
)
assert isinstance(type_info, mypy.types.CallableType)
self._precondition(
len(type_info.arg_types) == len(func_def.arguments),
"FuncDef argument type list length should match arguments list length",
func_loc,
)
self._break_label_stack = list[Label | None]()
# convert the return type
self._return_type = self.context.type_to_pytype(
type_info.ret_type, source_location=func_loc
)
return_wtype = self._return_type.checked_wtype(func_loc)
# check & convert the arguments
mypy_args = func_def.arguments
mypy_arg_types = type_info.arg_types
if type_info.def_extras.get("first_arg"):
# function is a method
if not mypy_args:
logger.error("method declaration is missing 'self' argument", location=func_loc)
else:
self._precondition(
mypy_args[0].variable.is_self,
"if function is a method, first variable should be self-like",
func_loc,
)
mypy_args = mypy_args[1:]
mypy_arg_types = mypy_arg_types[1:]
elif mypy_args:
self._precondition(
not mypy_args[0].variable.is_self,
"if function is not a method, first variable should be self-like",
func_loc,
)
self._symtable = dict[str, pytypes.PyType]()
args = list[SubroutineArgument]()
for arg, arg_type in zip(mypy_args, mypy_arg_types, strict=True):
arg_loc = self._location(arg)
if arg.kind.is_star():
raise CodeError("variadic functions are not supported", arg_loc)
if arg.initializer is not None:
self._error(
"default function argument values are not supported yet", arg.initializer
)
pytyp = self.context.type_to_pytype(arg_type, source_location=arg_loc)
wtype = pytyp.checked_wtype(arg_loc)
arg_name = arg.variable.name
args.append(SubroutineArgument(name=arg_name, wtype=wtype, source_location=arg_loc))
self._symtable[arg_name] = pytyp
# translate body
translated_body = self.visit_block(func_def.body)
# build result
self.result: Subroutine | ContractMethod
documentation = parse_docstring(func_def.docstring)
if self.contract_method_info is None:
self.result = Subroutine(
id=func_def.fullname,
name=func_def.name,
source_location=source_location,
args=args,
return_type=return_wtype,
body=translated_body,
documentation=documentation,
)
else:
self.result = ContractMethod(
cref=self.contract_method_info.fragment.id,
member_name=func_def.name,
source_location=source_location,
args=args,
return_type=return_wtype,
body=translated_body,
documentation=documentation,
arc4_method_config=self.contract_method_info.arc4_method_config,
)
@classmethod
@typing.overload
def convert(
cls,
context: ASTConversionModuleContext,
func_def: mypy.nodes.FuncDef,
source_location: SourceLocation,
) -> Subroutine: ...
@classmethod
@typing.overload
def convert(
cls,
context: ASTConversionModuleContext,
func_def: mypy.nodes.FuncDef,
source_location: SourceLocation,
contract_method_info: ContractMethodInfo,
) -> ContractMethod: ...
@classmethod
def convert(
cls,
context: ASTConversionModuleContext,
func_def: mypy.nodes.FuncDef,
source_location: SourceLocation,
contract_method_info: ContractMethodInfo | None = None,
) -> Subroutine | ContractMethod:
return cls(
context=context,
func_def=func_def,
contract_method_info=contract_method_info,
source_location=source_location,
).result
def visit_block(self, block: mypy.nodes.Block) -> Block:
translated_body = []
for stmt in block.body:
translated_stmt = stmt.accept(self)
if translated_stmt is None:
pass
elif isinstance(translated_stmt, Statement):
translated_body.append(translated_stmt)
else:
translated_body.extend(translated_stmt)
return Block(
source_location=self._location(block),
body=translated_body,
)
def visit_expression_stmt(self, stmt: mypy.nodes.ExpressionStmt) -> ExpressionStatement | None:
stmt_loc = self._location(stmt)
if isinstance(stmt.expr, mypy.nodes.StrExpr):
if stmt.expr.value != self.func_def.docstring:
logger.warning(
"String literal is not assigned and is not part of docstring",
location=stmt_loc,
)
return None
if isinstance(stmt.expr, mypy.nodes.TupleExpr) and len(stmt.expr.items) == 1:
raise CodeError(
"Tuple being constructed without assignment,"
" check for a stray comma at the end of the statement",
stmt_loc,
)
expr_builder = require_instance_builder(stmt.expr.accept(self))
if expr_builder.pytype != pytypes.NoneType:
if isinstance(stmt.expr, mypy.nodes.CallExpr) and isinstance(
stmt.expr.analyzed, mypy.nodes.RevealExpr | mypy.nodes.AssertTypeExpr
):
# special case to ignore ignoring the result of typing.reveal_type/assert_type
pass
elif isinstance(expr_builder.pytype, pytypes.InnerTransactionResultType) or (
isinstance(expr_builder.pytype, pytypes.TupleLikeType)
and any(
isinstance(i, pytypes.InnerTransactionResultType)
for i in expr_builder.pytype.items
)
):
# special case to ignore inner transaction result types
# could maybe expand this check to consider whether an expression has known
# side-effects
pass
else:
self.context.warning("expression result is ignored", stmt_loc)
return ExpressionStatement(expr=expr_builder.resolve())
def visit_assignment_stmt(self, stmt: mypy.nodes.AssignmentStmt) -> Sequence[Statement]:
stmt_loc = self._location(stmt)
match stmt.lvalues:
case [mypy.nodes.NameExpr(is_special_form=True)]:
self._error(
"type aliases, type vars, and type constructors"
" are not supported inside functions",
stmt_loc,
)
return []
match stmt.rvalue:
case mypy.nodes.TempNode(no_rhs=True):
assert (
len(stmt.lvalues) == 1
), "mypy should guarantee that an annotation assigment has only a single lvalue"
(lvalue,) = stmt.lvalues
assert (
stmt.type is not None
), "mypy should guarantee that an annotation assignment has a type set"
annotated_type = self.context.type_to_pytype(stmt.type, source_location=stmt_loc)
self._assign_type(lvalue, annotated_type)
# forward type-declaration only, no-op
return []
rvalue = require_instance_builder(stmt.rvalue.accept(self))
if not all(self._assign_type(lvalue, rvalue.pytype) for lvalue in stmt.lvalues):
# already a typing error, don't need a second one
return []
if isinstance(rvalue, StorageProxyConstructorResult):
try:
(lvalue,) = stmt.lvalues
except ValueError:
# this is true regardless of whether it's a self assignment or not,
# these are objects so aliasing is an issue in terms of semantic compatibility
raise CodeError(
f"{rvalue.pytype} can only be assigned to a single variable", stmt_loc
) from None
if is_self_member(lvalue):
return self._handle_proxy_assignment(lvalue, rvalue, stmt_loc)
elif rvalue.args.initial_value is not None:
raise CodeError(
"providing an initial value is only allowed"
" when assigning to a member variable",
stmt_loc,
)
elif isinstance(
rvalue.pytype, pytypes.StorageProxyType | pytypes.StorageMapProxyType
) and any(is_self_member(lvalue) for lvalue in stmt.lvalues):
raise CodeError("unsupported usage of storage type", stmt_loc)
elif len(stmt.lvalues) > 1:
rvalue = rvalue.single_eval()
return [
AssignmentStatement(
value=rvalue.resolve(),
target=self.resolve_lvalue(lvalue),
source_location=stmt_loc,
)
for lvalue in reversed(stmt.lvalues)
]
def _assign_type(
self,
lvalue: mypy.nodes.Expression,
typ: pytypes.PyType,
) -> bool:
match lvalue:
case mypy.nodes.NameExpr(name=var_name):
if var_name == "_":
raise CodeError(
"_ is not currently supported as a variable name", self._location(lvalue)
)
lvalue_loc = self._location(lvalue)
symbol_type = self._symtable.setdefault(var_name, typ)
if not (symbol_type <= typ):
logger.error(
f"{var_name!r} already has type {symbol_type}"
f" which is not compatible with {typ}",
location=lvalue_loc,
)
return False
return True
case mypy.nodes.TupleExpr(items=lval_items) | mypy.nodes.ListExpr(items=lval_items):
if star_expr := next(
(
lval_item
for lval_item in lval_items
if isinstance(lval_item, mypy.nodes.StarExpr)
),
None,
):
raise CodeError(
"star expressions are not supported", self._location(star_expr)
)
match typ:
case pytypes.SequenceType(items=homogenous_type):
tuple_item_types = (homogenous_type,) * len(lval_items)
case pytypes.TupleLikeType(items=tuple_item_types):
if len(tuple_item_types) != len(lval_items):
raise CodeError(
f"length mismatch source size of {len(tuple_item_types)}"
f" and target size of {len(lval_items)}",
self._location(lvalue),
)
case _:
raise CodeError(
"source type not supported for unpacking", self._location(lvalue)
)
return all(
self._assign_type(lval_item, inner_typ)
for lval_item, inner_typ in zip(lval_items, tuple_item_types, strict=True)
)
case _:
return True
def _handle_proxy_assignment(
self,
lvalue: mypy.nodes.MemberExpr,
rvalue: StorageProxyConstructorResult,
stmt_loc: SourceLocation,
) -> Sequence[Statement]:
if self.contract_method_info is None:
raise InternalError("Assignment to self outside of a contract class", stmt_loc)
pytype = rvalue.pytype
if self.func_def.name != "__init__":
raise CodeError(
f"{pytype.generic or pytype}"
" can only be assigned to a member variable in the __init__ method",
stmt_loc,
)
member_loc = self._location(lvalue)
member_name = lvalue.name
storage = self.contract_method_info.fragment.resolve_storage(member_name)
if storage is None:
raise CodeError("unable to resolve class storage member", member_loc)
if storage.definition is not None:
logger.info(
f"previous definition of {member_name} was here",
location=storage.definition.source_location,
)
logger.error(
f"redefinition of {member_name}",
location=stmt_loc,
)
key = rvalue.args.key
if key is None:
key = BytesConstant(
value=member_name.encode("utf8"),
wtype=pytype.wtype,
encoding=BytesEncoding.utf8,
source_location=member_loc,
)
elif not isinstance(key, BytesConstant):
logger.error(
f"assigning {pytype} to a member variable"
f" requires a constant value for {rvalue.args.key_arg_name}",
location=stmt_loc,
)
key = BytesConstant(
value=b"0",
wtype=key.wtype,
encoding=BytesEncoding.unknown,
source_location=key.source_location,
)
storage.definition = AppStorageDefinition(
source_location=member_loc,
member_name=member_name,
kind=storage.kind,
storage_wtype=pytype.content_wtype,
key_wtype=(
pytype.key_wtype if isinstance(pytype, pytypes.StorageMapProxyType) else None
),
key=key,
description=rvalue.args.description,
)
if rvalue.args.initial_value is None:
return []
match pytype:
case pytypes.StorageProxyType(
generic=pytypes.GenericGlobalStateType, content_wtype=content_wtype
):
global_state_target = AppStateExpression(
key=key,
exists_assertion_message=None, # this is a write, not a read
wtype=content_wtype,
source_location=member_loc,
)
return [
AssignmentStatement(
target=global_state_target,
value=rvalue.args.initial_value.resolve(),
source_location=stmt_loc,
)
]
case unhandled:
raise InternalError(
f"Don't know how to do initialise-on-declaration for {unhandled}", stmt_loc
)
def resolve_lvalue(self, lvalue: mypy.nodes.Expression) -> Lvalue:
builder_or_literal = lvalue.accept(self)
builder = require_instance_builder(builder_or_literal)
return builder.resolve_lvalue()
@typing.override
def empty_statement(self, stmt: mypy.nodes.Statement) -> None:
return None
def visit_operator_assignment_stmt(self, stmt: mypy.nodes.OperatorAssignmentStmt) -> Statement:
stmt_loc = self._location(stmt)
builder = require_instance_builder(stmt.lvalue.accept(self))
rhs = require_instance_builder(stmt.rvalue.accept(self))
try:
op = BuilderBinaryOp(stmt.op)
except ValueError as ex:
raise InternalError(f"Unknown binary operator: {stmt.op}") from ex
return builder.augmented_assignment(op=op, rhs=rhs, location=stmt_loc)
def visit_if_stmt(self, stmt: mypy.nodes.IfStmt) -> IfElse:
self._precondition(
len(stmt.body) == len(stmt.expr),
"mismatch between if statement condition list length and body list length",
stmt,
)
self._precondition(
len(stmt.expr) == 1,
"python ast module should produce normalised if statements",
stmt,
)
(expr,) = stmt.expr
(if_body,) = stmt.body
condition = expr.accept(self).bool_eval(self._location(expr))
if_branch = self.visit_block(if_body)
else_branch = self.visit_block(stmt.else_body) if stmt.else_body else None
return IfElse(
source_location=self._location(stmt),
condition=condition.resolve(),
if_branch=if_branch,
else_branch=else_branch,
)
@typing.overload
def _build_loop(
self,
stmt: mypy.nodes.WhileStmt,
ctor: Callable[[Block, SourceLocation], WhileLoop],
) -> WhileLoop | Block: ...
@typing.overload
def _build_loop(
self,
stmt: mypy.nodes.ForStmt,
ctor: Callable[[Block, SourceLocation], ForInLoop],
) -> ForInLoop | Block: ...
def _build_loop(
self,
stmt: mypy.nodes.WhileStmt | mypy.nodes.ForStmt,
ctor: Callable[[Block, SourceLocation], WhileLoop | ForInLoop],
) -> WhileLoop | ForInLoop | Block:
loop_loc = self._location(stmt)
if stmt.else_body is None:
break_label = None
else:
break_label = Label(f"after_loop_L{loop_loc.line}")
self._break_label_stack.append(break_label)
try:
block = self.visit_block(stmt.body)
finally:
self._break_label_stack.pop()
loop = ctor(block, loop_loc)
if stmt.else_body is None:
return loop
else_body = self.visit_block(stmt.else_body)
# empty block, exists so we have a goto target that skips the else body
after_block = Block(label=break_label, body=[], source_location=loop_loc)
return Block(
body=[loop, else_body, after_block],
comment=f"loop_with_else_L{loop_loc.line}",
source_location=loop_loc,
)
def visit_while_stmt(self, stmt: mypy.nodes.WhileStmt) -> WhileLoop | Block:
condition = stmt.expr.accept(self).bool_eval(self._location(stmt.expr))
return self._build_loop(
stmt,
lambda loop_body, loop_loc: WhileLoop(
condition=condition.resolve(),
loop_body=loop_body,
source_location=loop_loc,
),
)
def visit_for_stmt(self, stmt: mypy.nodes.ForStmt) -> ForInLoop | Block:
sequence_builder = require_instance_builder(stmt.expr.accept(self))
sequence = sequence_builder.iterate()
iter_item_type = sequence_builder.iterable_item_type()
self._assign_type(stmt.index, iter_item_type)
items = self.resolve_lvalue(stmt.index)
return self._build_loop(
stmt,
lambda loop_body, loop_loc: ForInLoop(
items=items,
sequence=sequence,
loop_body=loop_body,
source_location=loop_loc,
),
)
def visit_break_stmt(self, stmt: mypy.nodes.BreakStmt) -> LoopExit | Goto:
stmt_loc = self._location(stmt)
break_label = self._break_label_stack[-1]
if break_label is None:
return LoopExit(stmt_loc)
else:
return Goto(target=break_label, source_location=stmt_loc)
def visit_continue_stmt(self, stmt: mypy.nodes.ContinueStmt) -> LoopContinue:
return LoopContinue(self._location(stmt))
def visit_assert_stmt(self, stmt: mypy.nodes.AssertStmt) -> ExpressionStatement:
error_message: str | None = None
if stmt.msg is not None:
msg = stmt.msg.accept(self)
match msg:
case LiteralBuilder(value=str(error_message)):
pass
case _:
self._error("only literal strings are supported as assertion messages", stmt)
condition = stmt.expr.accept(self).bool_eval(self._location(stmt.expr))
return ExpressionStatement(
AssertExpression(
condition=condition.resolve(),
error_message=error_message,
source_location=self._location(stmt),
)
)
def visit_del_stmt(self, stmt: mypy.nodes.DelStmt) -> Statement:
stmt_expr = stmt.expr.accept(self)
del_item = require_instance_builder(stmt_expr)
return del_item.delete(self._location(stmt))
def visit_return_stmt(self, stmt: mypy.nodes.ReturnStmt) -> ReturnStatement | None:
loc = self._location(stmt)
return_expr = stmt.expr
if return_expr is None:
if self._return_type is not pytypes.NoneType:
self._error(
"function is typed as returning a value, so a value must be returned", loc
)
return ReturnStatement(source_location=loc, value=None)
returning_builder = require_instance_builder(return_expr.accept(self))
if not (self._return_type <= returning_builder.pytype):
self._error(
f"invalid return type of {returning_builder.pytype}, expected {self._return_type}",
loc,
)
return ReturnStatement(source_location=loc, value=returning_builder.resolve())
def visit_match_stmt(self, stmt: mypy.nodes.MatchStmt) -> Switch | None:
loc = self._location(stmt)
subject = require_instance_builder(stmt.subject.accept(self))
case_block_map = dict[Expression, Block]()
default_block: Block | None = None
for pattern, guard, block in zip(stmt.patterns, stmt.guards, stmt.bodies, strict=True):
if guard is not None:
self._error("guard clauses are not supported", guard)
if default_block is not None:
self._error("default case already encountered", pattern)
continue
case_block = self.visit_block(block)
pattern_loc = self._location(pattern)
case_value_builder: InstanceBuilder | None = None
match pattern:
case mypy.patterns.AsPattern(name=None, pattern=None):
default_block = case_block
case mypy.patterns.ValuePattern(expr=case_expr):
case_value_builder = require_instance_builder(case_expr.accept(self))
case_value_builder = maybe_resolve_literal(case_value_builder, subject.pytype)
case mypy.patterns.SingletonPattern(value=bool(bool_literal)):
case_value_builder = LiteralBuilderImpl(
value=bool_literal, source_location=pattern_loc
)
case_value_builder = maybe_resolve_literal(case_value_builder, subject.pytype)
case mypy.patterns.ClassPattern() as cls_pattern:
class_builder = cls_pattern.class_ref.accept(self)
if not isinstance(class_builder, CallableBuilder):
logger.error("expected a type", location=class_builder.source_location)
elif cls_pattern.keyword_values:
self._error(
"attribute matching is not supported", cls_pattern.keyword_values[0]
)
else:
cls_args = []
has_error = False
for pos_case in cls_pattern.positionals:
if isinstance(pos_case, mypy.patterns.ValuePattern):
cls_args.append(pos_case.expr.accept(self))
else:
has_error = True
self._error("expected a value pattern", pos_case)
if not has_error:
case_value_builder = class_builder.call(
args=cls_args,
arg_kinds=[mypy.nodes.ARG_POS] * len(cls_args),
arg_names=[None] * len(cls_args),
location=pattern_loc,
)
case _:
logger.error("unsupported case pattern", location=pattern_loc)
if case_value_builder is not None:
if case_value_builder.pytype != subject.pytype:
# TODO: what about other comparable types, or subtypes?
logger.error(
"type mismatch,"
" case values must be the exact same type as the subject type",
location=pattern_loc,
)
else:
case_block_map[case_value_builder.resolve()] = case_block
return Switch(
source_location=loc,
value=subject.resolve(),
cases=case_block_map,
default_case=default_block,
)
# Unsupported statements
def visit_function(self, fdef: mypy.nodes.FuncDef, _: mypy.nodes.Decorator | None) -> None:
self._error("nested functions are not supported", fdef)
def visit_class_def(self, cdef: mypy.nodes.ClassDef) -> None:
self._error("classes nested inside functions are not supported", cdef)
# Expressions
def _visit_ref_expr(self, expr: mypy.nodes.MemberExpr | mypy.nodes.NameExpr) -> NodeBuilder:
expr_loc = self._location(expr)
# Do a straight forward lookup at the RefExpr level handle the cases of:
# - type aliases
# - simple (non-generic) types
# - generic type that has not been parameterised
# (e.g. when calling a constructor which can infer the type from its arguments)
# For parameterised generics, these are resolved at the IndexExpr level, without
# descending into IndexExpr.base.
# By doing a simple lookup instead of resolving the PyType of expr,
# we can side step complex constructs in the stubs that we don't support in user code,
# such as overloads.
if py_typ := self.context.lookup_pytype(expr.fullname):
if isinstance(py_typ, pytypes.ContractType):
if fragment := self.context.contract_fragments.get(py_typ.name):
return ContractTypeExpressionBuilder(py_typ, fragment, expr_loc)
elif pytypes.ARC4ClientBaseType < py_typ: # provides type info only
if fragment := self.context.contract_fragments.get(ContractReference(py_typ.name)):
return ARC4ClientTypeBuilder(py_typ, expr_loc, fragment)
elif py_typ == pytypes.LogicSigType:
ref = LogicSigReference(get_unaliased_fullname(expr))
return LogicSigExpressionBuilder(ref, expr_loc)
else:
return builder_for_type(py_typ, expr_loc)
if expr.name == "__all__":
# special case here, we allow __all__ at the module level for it's "public vs private"
# control implications w.r.t linting etc, but we do so by ignoring it.
# so this is here just in case someone tries to reference __all__ inside a function,
# to give a more useful error message.
raise CodeError("__all__ cannot be referenced inside functions", expr_loc)
fullname = get_unaliased_fullname(expr)
if fullname.startswith("builtins."):
return self._visit_ref_expr_of_builtins(fullname, expr_loc)
if func_builder := try_get_builder_for_func(fullname, expr_loc):
return func_builder
match expr:
case mypy.nodes.NameExpr(node=mypy.nodes.Var(is_self=True)):
if self.contract_method_info is None:
raise InternalError(
"variable is inferred as self outside of contract scope", expr_loc
)
return ContractSelfExpressionBuilder(
fragment=self.contract_method_info.fragment,
pytype=self.contract_method_info.contract_type,
location=expr_loc,
)
case mypy.nodes.RefExpr(
node=mypy.nodes.Decorator(decorators=decorators) as dec
) if any(
get_unaliased_fullname(de) == constants.SUBROUTINE_HINT
for de in decorators
if isinstance(de, mypy.nodes.RefExpr) # TODO: why wouldn't this be a RefExpr
):
self._precondition(
expr.fullname != expr.name,
"unqualified name found in call to function",
expr_loc,
)
mypy_func_type = require_callable_type(dec, expr_loc)
func_type = self.context.type_to_pytype(mypy_func_type, source_location=expr_loc)
if not isinstance(func_type, pytypes.FuncType):
raise CodeError("decorated function has non-function type", expr_loc)
return SubroutineInvokerExpressionBuilder(
target=SubroutineID(expr.fullname),
func_type=func_type,
location=expr_loc,
)
case mypy.nodes.RefExpr(node=mypy.nodes.FuncDef()):
raise CodeError(
f"Cannot invoke {fullname} as it is not "
f"decorated with {constants.SUBROUTINE_HINT_ALIAS}",
expr_loc,
)
# TODO: is this enough to filter to only global variable references?
# it seems like it should be...
case mypy.nodes.RefExpr(kind=mypy.nodes.GDEF, node=mypy.nodes.Var()):
self._precondition(
not (
expr.is_new_def
or expr.is_inferred_def
or expr.is_alias_rvalue
or (isinstance(expr, mypy.nodes.NameExpr) and expr.is_special_form)
),
"global variable reference with unexpected flags",
expr_loc,
)
try:
constant_value = self.context.constants[expr.fullname]
except KeyError as ex:
raise CodeError(
"Unable to resolve global constant reference", expr_loc
) from ex
else:
return LiteralBuilderImpl(source_location=expr_loc, value=constant_value)
case (
mypy.nodes.NameExpr(
kind=mypy.nodes.LDEF, node=mypy.nodes.Var(), name=var_name
) as name_expr
):
self._precondition(
not name_expr.is_special_form,
"special form lvalues should only appear"
" as a singular lvalue in an assignment statement",
expr_loc,
)
local_type = self._symtable.get(var_name)
if local_type is None:
raise CodeError(
"local variable type is unknown - possible use before definition?",
expr_loc,
)
var_expr = VarExpression(
name=var_name,
wtype=local_type.checked_wtype(expr_loc),
source_location=expr_loc,
)
return builder_for_instance(local_type, var_expr)
scope = {
mypy.nodes.LDEF: "local",
mypy.nodes.MDEF: "member",
mypy.nodes.GDEF: "global",
None: "unknown",
}.get(expr.kind)
# this can happen in otherwise well-formed code that is just missing a reference
raise CodeError(
f"Unable to resolve reference to {expr.fullname or expr.name!r}, {scope=}",
expr_loc,
)
@staticmethod
def _visit_ref_expr_of_builtins(fullname: str, location: SourceLocation) -> NodeBuilder:
assert fullname.startswith("builtins.")
rest_of_name = fullname.removeprefix("builtins.")
match rest_of_name:
case "True":
return LiteralBuilderImpl(source_location=location, value=True)
case "False":
return LiteralBuilderImpl(source_location=location, value=False)
case "None":
raise CodeError("None is not supported as a value, only a return type", location)
case "len":
raise CodeError(
"len() is not supported -"
" types with a length will have a .length property instead",
location,
)
case "range":
raise CodeError("range() is not supported - use algopy.urange() instead", location)
case "enumerate":
raise CodeError(
"enumerate() is not supported - use algopy.uenumerate() instead", location
)
case _:
raise CodeError(f"Unsupported builtin: {rest_of_name}", location)
def visit_name_expr(self, expr: mypy.nodes.NameExpr) -> NodeBuilder:
return self._visit_ref_expr(expr)
def visit_member_expr(self, expr: mypy.nodes.MemberExpr) -> NodeBuilder:
expr_loc = self._location(expr)
if isinstance(expr.expr, mypy.nodes.RefExpr) and isinstance(
expr.expr.node, mypy.nodes.MypyFile
):
# special case for module attribute access
return self._visit_ref_expr(expr)
base = expr.expr.accept(self)
return base.member_access(expr.name, expr_loc)
def visit_call_expr(self, call: mypy.nodes.CallExpr) -> NodeBuilder:
if call.analyzed is not None:
return self._visit_special_call_expr(call, analyzed=call.analyzed)
callee = call.callee.accept(self)
if not isinstance(callee, CallableBuilder):
raise CodeError("not a callable expression", self._location(call.callee))
args = [arg.accept(self) for arg in call.args]
return callee.call(
args=args,
arg_kinds=call.arg_kinds,
arg_names=call.arg_names,
location=self._location(call),
)
def _visit_special_call_expr(
self, call: mypy.nodes.CallExpr, *, analyzed: mypy.nodes.Expression
) -> NodeBuilder:
match analyzed:
case mypy.nodes.CastExpr(expr=inner_expr):
self.context.warning(
"use of typing.cast, output may be invalid or insecure TEAL", call
)
case mypy.nodes.AssertTypeExpr(expr=inner_expr):
# just FYI... in case the user thinks this has a runtime effect
# (it doesn't, not even in Python)
self.context.warning(
"use of typing.assert_type has no effect on compilation", call
)
case mypy.nodes.RevealExpr(expr=mypy.nodes.Expression() as inner_expr):
pass
case _:
raise CodeError(
f"Unsupported special function call"
f" of analyzed type {type(analyzed).__name__}",
self._location(call),
)
return inner_expr.accept(self)
def visit_unary_expr(self, node: mypy.nodes.UnaryExpr) -> NodeBuilder:
expr_loc = self._location(node)
builder_or_literal = node.expr.accept(self)
match node.op:
case "not":
return builder_or_literal.bool_eval(expr_loc, negate=True)
case op_str if op_str in BuilderUnaryOp:
builder_op = BuilderUnaryOp(op_str)
return require_instance_builder(builder_or_literal).unary_op(builder_op, expr_loc)
case _:
# guard against future python unary operators
raise InternalError(f"Unable to interpret unary operator '{node.op}'", expr_loc)
def visit_op_expr(self, node: mypy.nodes.OpExpr) -> NodeBuilder:
node_loc = self._location(node)
lhs = require_instance_builder(node.left.accept(self))
rhs = require_instance_builder(node.right.accept(self))
# mypy combines ast.BoolOp and ast.BinOp, but they're kinda different...
if node.op in BinaryBooleanOperator:
bool_op = BinaryBooleanOperator(node.op)
return lhs.bool_binary_op(rhs, bool_op, node_loc)
try:
op = BuilderBinaryOp(node.op)
except ValueError as ex:
raise InternalError(f"Unknown binary operator: {node.op}") from ex
result: NodeBuilder = NotImplemented
if isinstance(lhs, NodeBuilder):
result = lhs.binary_op(other=rhs, op=op, location=node_loc, reverse=False)
if result is NotImplemented and isinstance(rhs, NodeBuilder):
result = rhs.binary_op(other=lhs, op=op, location=node_loc, reverse=True)
if result is NotImplemented:
raise CodeError(f"unsupported operation {op.value} between types", node_loc)
return result
def visit_index_expr(self, expr: mypy.nodes.IndexExpr) -> NodeBuilder:
expr_location = self._location(expr)
if isinstance(expr.method_type, mypy.types.CallableType):
result_pytyp = self.context.type_to_pytype(
expr.method_type.ret_type, source_location=expr_location
)
if isinstance(result_pytyp, pytypes.PseudoGenericFunctionType):
return builder_for_type(result_pytyp, expr_location)
match expr.analyzed:
case None:
pass
case mypy.nodes.TypeAliasExpr():
raise CodeError("type aliases are not supported inside subroutines", expr_location)
case mypy.nodes.TypeApplication(types=type_args):
# TODO: resolve expr to generic type builder instead
assert isinstance(
expr.base, mypy.nodes.RefExpr
), "expect base of type application to be RefExpr"
pytyp = self.context.lookup_pytype(expr.base.fullname)
if pytyp is None:
raise CodeError("unknown base type", expr_location)
param_typ = pytyp.parameterise(
[
self.context.type_to_pytype(
t, in_type_args=True, source_location=expr_location
)
for t in type_args
],
expr_location,
)