From ce3f61fb55507f9e0734cdc971cb3a2b7178f407 Mon Sep 17 00:00:00 2001 From: tserg <8017125+tserg@users.noreply.github.com> Date: Thu, 21 Dec 2023 19:14:49 +0800 Subject: [PATCH] rename evaluate to fold --- tests/functional/builtins/folding/test_abs.py | 2 +- .../builtins/folding/test_addmod_mulmod.py | 2 +- .../builtins/folding/test_bitwise.py | 8 +-- .../builtins/folding/test_epsilon.py | 2 +- .../builtins/folding/test_floor_ceil.py | 2 +- .../folding/test_fold_as_wei_value.py | 4 +- .../builtins/folding/test_keccak_sha.py | 6 +-- tests/functional/builtins/folding/test_len.py | 6 +-- .../builtins/folding/test_min_max.py | 6 +-- .../builtins/folding/test_powmod.py | 2 +- ..._decimal.py => test_fold_binop_decimal.py} | 4 +- ...te_binop_int.py => test_fold_binop_int.py} | 6 +-- ...evaluate_boolop.py => test_fold_boolop.py} | 2 +- ...aluate_compare.py => test_fold_compare.py} | 10 ++-- ...te_subscript.py => test_fold_subscript.py} | 2 +- ...aluate_unaryop.py => test_fold_unaryop.py} | 2 +- vyper/ast/README.md | 3 +- vyper/ast/folding.py | 8 +-- vyper/ast/nodes.py | 16 +++--- vyper/ast/nodes.pyi | 2 +- vyper/builtins/_signatures.py | 4 +- vyper/builtins/functions.py | 50 +++++++++---------- 22 files changed, 74 insertions(+), 75 deletions(-) rename tests/unit/ast/nodes/{test_evaluate_binop_decimal.py => test_fold_binop_decimal.py} (97%) rename tests/unit/ast/nodes/{test_evaluate_binop_int.py => test_fold_binop_int.py} (96%) rename tests/unit/ast/nodes/{test_evaluate_boolop.py => test_fold_boolop.py} (98%) rename tests/unit/ast/nodes/{test_evaluate_compare.py => test_fold_compare.py} (95%) rename tests/unit/ast/nodes/{test_evaluate_subscript.py => test_fold_subscript.py} (94%) rename tests/unit/ast/nodes/{test_evaluate_unaryop.py => test_fold_unaryop.py} (96%) diff --git a/tests/functional/builtins/folding/test_abs.py b/tests/functional/builtins/folding/test_abs.py index 5929ef458b..339b549135 100644 --- a/tests/functional/builtins/folding/test_abs.py +++ b/tests/functional/builtins/folding/test_abs.py @@ -21,7 +21,7 @@ def foo(a: int256) -> int256: vyper_ast = vy_ast.parse_to_ast(f"abs({a})") old_node = vyper_ast.body[0].value - new_node = vy_fn.DISPATCH_TABLE["abs"].evaluate(old_node) + new_node = vy_fn.DISPATCH_TABLE["abs"].fold(old_node) assert contract.foo(a) == new_node.value == abs(a) diff --git a/tests/functional/builtins/folding/test_addmod_mulmod.py b/tests/functional/builtins/folding/test_addmod_mulmod.py index 33dcc62984..03325240a5 100644 --- a/tests/functional/builtins/folding/test_addmod_mulmod.py +++ b/tests/functional/builtins/folding/test_addmod_mulmod.py @@ -24,6 +24,6 @@ def foo(a: uint256, b: uint256, c: uint256) -> uint256: vyper_ast = vy_ast.parse_to_ast(f"{fn_name}({a}, {b}, {c})") old_node = vyper_ast.body[0].value - new_node = vy_fn.DISPATCH_TABLE[fn_name].evaluate(old_node) + new_node = vy_fn.DISPATCH_TABLE[fn_name].fold(old_node) assert contract.foo(a, b, c) == new_node.value diff --git a/tests/functional/builtins/folding/test_bitwise.py b/tests/functional/builtins/folding/test_bitwise.py index 63e733644f..d5c0f1ac86 100644 --- a/tests/functional/builtins/folding/test_bitwise.py +++ b/tests/functional/builtins/folding/test_bitwise.py @@ -28,7 +28,7 @@ def foo(a: uint256, b: uint256) -> uint256: vyper_ast = vy_ast.parse_to_ast(f"{a} {op} {b}") old_node = vyper_ast.body[0].value - new_node = old_node.evaluate() + new_node = old_node.fold() assert contract.foo(a, b) == new_node.value @@ -49,7 +49,7 @@ def foo(a: uint256, b: uint256) -> uint256: old_node = vyper_ast.body[0].value try: - new_node = old_node.evaluate() + new_node = old_node.fold() # force bounds check, no-op because validate_numeric_bounds # already does this, but leave in for hygiene (in case # more types are added). @@ -79,7 +79,7 @@ def foo(a: int256, b: uint256) -> int256: old_node = vyper_ast.body[0].value try: - new_node = old_node.evaluate() + new_node = old_node.fold() validate_expected_type(new_node, INT256_T) # force bounds check # compile time behavior does not match runtime behavior. # compile-time will throw on OOB, runtime will wrap. @@ -104,6 +104,6 @@ def foo(a: uint256) -> uint256: vyper_ast = vy_ast.parse_to_ast(f"~{value}") old_node = vyper_ast.body[0].value - new_node = old_node.evaluate() + new_node = old_node.fold() assert contract.foo(value) == new_node.value diff --git a/tests/functional/builtins/folding/test_epsilon.py b/tests/functional/builtins/folding/test_epsilon.py index 794648cfce..3110d5eae5 100644 --- a/tests/functional/builtins/folding/test_epsilon.py +++ b/tests/functional/builtins/folding/test_epsilon.py @@ -15,6 +15,6 @@ def foo() -> {typ_name}: vyper_ast = vy_ast.parse_to_ast(f"epsilon({typ_name})") old_node = vyper_ast.body[0].value - new_node = vy_fn.DISPATCH_TABLE["epsilon"].evaluate(old_node) + new_node = vy_fn.DISPATCH_TABLE["epsilon"].fold(old_node) assert contract.foo() == new_node.value diff --git a/tests/functional/builtins/folding/test_floor_ceil.py b/tests/functional/builtins/folding/test_floor_ceil.py index 87db23889a..8e3f14e9ec 100644 --- a/tests/functional/builtins/folding/test_floor_ceil.py +++ b/tests/functional/builtins/folding/test_floor_ceil.py @@ -30,6 +30,6 @@ def foo(a: decimal) -> int256: vyper_ast = vy_ast.parse_to_ast(f"{fn_name}({value})") old_node = vyper_ast.body[0].value - new_node = vy_fn.DISPATCH_TABLE[fn_name].evaluate(old_node) + new_node = vy_fn.DISPATCH_TABLE[fn_name].fold(old_node) assert contract.foo(value) == new_node.value diff --git a/tests/functional/builtins/folding/test_fold_as_wei_value.py b/tests/functional/builtins/folding/test_fold_as_wei_value.py index 210ab51f0d..9af1618bcb 100644 --- a/tests/functional/builtins/folding/test_fold_as_wei_value.py +++ b/tests/functional/builtins/folding/test_fold_as_wei_value.py @@ -32,7 +32,7 @@ def foo(a: decimal) -> uint256: vyper_ast = vy_ast.parse_to_ast(f"as_wei_value({value:.10f}, '{denom}')") old_node = vyper_ast.body[0].value - new_node = vy_fn.AsWeiValue().evaluate(old_node) + new_node = vy_fn.AsWeiValue().fold(old_node) assert contract.foo(value) == new_node.value @@ -51,6 +51,6 @@ def foo(a: uint256) -> uint256: vyper_ast = vy_ast.parse_to_ast(f"as_wei_value({value}, '{denom}')") old_node = vyper_ast.body[0].value - new_node = vy_fn.AsWeiValue().evaluate(old_node) + new_node = vy_fn.AsWeiValue().fold(old_node) assert contract.foo(value) == new_node.value diff --git a/tests/functional/builtins/folding/test_keccak_sha.py b/tests/functional/builtins/folding/test_keccak_sha.py index a2fe460dd1..768a46a40d 100644 --- a/tests/functional/builtins/folding/test_keccak_sha.py +++ b/tests/functional/builtins/folding/test_keccak_sha.py @@ -22,7 +22,7 @@ def foo(a: String[100]) -> bytes32: vyper_ast = vy_ast.parse_to_ast(f"{fn_name}('''{value}''')") old_node = vyper_ast.body[0].value - new_node = vy_fn.DISPATCH_TABLE[fn_name].evaluate(old_node) + new_node = vy_fn.DISPATCH_TABLE[fn_name].fold(old_node) assert f"0x{contract.foo(value).hex()}" == new_node.value @@ -41,7 +41,7 @@ def foo(a: Bytes[100]) -> bytes32: vyper_ast = vy_ast.parse_to_ast(f"{fn_name}({value})") old_node = vyper_ast.body[0].value - new_node = vy_fn.DISPATCH_TABLE[fn_name].evaluate(old_node) + new_node = vy_fn.DISPATCH_TABLE[fn_name].fold(old_node) assert f"0x{contract.foo(value).hex()}" == new_node.value @@ -62,6 +62,6 @@ def foo(a: Bytes[100]) -> bytes32: vyper_ast = vy_ast.parse_to_ast(f"{fn_name}({value})") old_node = vyper_ast.body[0].value - new_node = vy_fn.DISPATCH_TABLE[fn_name].evaluate(old_node) + new_node = vy_fn.DISPATCH_TABLE[fn_name].fold(old_node) assert f"0x{contract.foo(value).hex()}" == new_node.value diff --git a/tests/functional/builtins/folding/test_len.py b/tests/functional/builtins/folding/test_len.py index edf33120dd..f4d54e202b 100644 --- a/tests/functional/builtins/folding/test_len.py +++ b/tests/functional/builtins/folding/test_len.py @@ -17,7 +17,7 @@ def foo(a: String[1024]) -> uint256: vyper_ast = vy_ast.parse_to_ast(f"len('{value}')") old_node = vyper_ast.body[0].value - new_node = vy_fn.Len().evaluate(old_node) + new_node = vy_fn.Len().fold(old_node) assert contract.foo(value) == new_node.value @@ -35,7 +35,7 @@ def foo(a: Bytes[1024]) -> uint256: vyper_ast = vy_ast.parse_to_ast(f"len(b'{value}')") old_node = vyper_ast.body[0].value - new_node = vy_fn.Len().evaluate(old_node) + new_node = vy_fn.Len().fold(old_node) assert contract.foo(value.encode()) == new_node.value @@ -53,6 +53,6 @@ def foo(a: Bytes[1024]) -> uint256: vyper_ast = vy_ast.parse_to_ast(f"len({value})") old_node = vyper_ast.body[0].value - new_node = vy_fn.Len().evaluate(old_node) + new_node = vy_fn.Len().fold(old_node) assert contract.foo(value) == new_node.value diff --git a/tests/functional/builtins/folding/test_min_max.py b/tests/functional/builtins/folding/test_min_max.py index 309f7519c0..4548e482ca 100644 --- a/tests/functional/builtins/folding/test_min_max.py +++ b/tests/functional/builtins/folding/test_min_max.py @@ -31,7 +31,7 @@ def foo(a: decimal, b: decimal) -> decimal: vyper_ast = vy_ast.parse_to_ast(f"{fn_name}({left}, {right})") old_node = vyper_ast.body[0].value - new_node = vy_fn.DISPATCH_TABLE[fn_name].evaluate(old_node) + new_node = vy_fn.DISPATCH_TABLE[fn_name].fold(old_node) assert contract.foo(left, right) == new_node.value @@ -50,7 +50,7 @@ def foo(a: int128, b: int128) -> int128: vyper_ast = vy_ast.parse_to_ast(f"{fn_name}({left}, {right})") old_node = vyper_ast.body[0].value - new_node = vy_fn.DISPATCH_TABLE[fn_name].evaluate(old_node) + new_node = vy_fn.DISPATCH_TABLE[fn_name].fold(old_node) assert contract.foo(left, right) == new_node.value @@ -69,6 +69,6 @@ def foo(a: uint256, b: uint256) -> uint256: vyper_ast = vy_ast.parse_to_ast(f"{fn_name}({left}, {right})") old_node = vyper_ast.body[0].value - new_node = vy_fn.DISPATCH_TABLE[fn_name].evaluate(old_node) + new_node = vy_fn.DISPATCH_TABLE[fn_name].fold(old_node) assert contract.foo(left, right) == new_node.value diff --git a/tests/functional/builtins/folding/test_powmod.py b/tests/functional/builtins/folding/test_powmod.py index 8667ec93fd..f531e77af6 100644 --- a/tests/functional/builtins/folding/test_powmod.py +++ b/tests/functional/builtins/folding/test_powmod.py @@ -21,6 +21,6 @@ def foo(a: uint256, b: uint256) -> uint256: vyper_ast = vy_ast.parse_to_ast(f"pow_mod256({a}, {b})") old_node = vyper_ast.body[0].value - new_node = vy_fn.PowMod256().evaluate(old_node) + new_node = vy_fn.PowMod256().fold(old_node) assert contract.foo(a, b) == new_node.value diff --git a/tests/unit/ast/nodes/test_evaluate_binop_decimal.py b/tests/unit/ast/nodes/test_fold_binop_decimal.py similarity index 97% rename from tests/unit/ast/nodes/test_evaluate_binop_decimal.py rename to tests/unit/ast/nodes/test_fold_binop_decimal.py index 5c9956caba..f20e422574 100644 --- a/tests/unit/ast/nodes/test_evaluate_binop_decimal.py +++ b/tests/unit/ast/nodes/test_fold_binop_decimal.py @@ -31,7 +31,7 @@ def foo(a: decimal, b: decimal) -> decimal: vyper_ast = vy_ast.parse_to_ast(f"{left} {op} {right}") old_node = vyper_ast.body[0].value try: - new_node = old_node.evaluate() + new_node = old_node.fold() is_valid = True except ZeroDivisionException: is_valid = False @@ -48,7 +48,7 @@ def test_binop_pow(): old_node = vyper_ast.body[0].value with pytest.raises(TypeMismatch): - old_node.evaluate() + old_node.fold() @pytest.mark.fuzzing diff --git a/tests/unit/ast/nodes/test_evaluate_binop_int.py b/tests/unit/ast/nodes/test_fold_binop_int.py similarity index 96% rename from tests/unit/ast/nodes/test_evaluate_binop_int.py rename to tests/unit/ast/nodes/test_fold_binop_int.py index 80c9381c0f..62a5ec4d53 100644 --- a/tests/unit/ast/nodes/test_evaluate_binop_int.py +++ b/tests/unit/ast/nodes/test_fold_binop_int.py @@ -27,7 +27,7 @@ def foo(a: int128, b: int128) -> int128: vyper_ast = vy_ast.parse_to_ast(f"{left} {op} {right}") old_node = vyper_ast.body[0].value try: - new_node = old_node.evaluate() + new_node = old_node.fold() is_valid = True except ZeroDivisionException: is_valid = False @@ -56,7 +56,7 @@ def foo(a: uint256, b: uint256) -> uint256: vyper_ast = vy_ast.parse_to_ast(f"{left} {op} {right}") old_node = vyper_ast.body[0].value try: - new_node = old_node.evaluate() + new_node = old_node.fold() is_valid = new_node.value >= 0 except ZeroDivisionException: is_valid = False @@ -83,7 +83,7 @@ def foo(a: uint256, b: uint256) -> uint256: vyper_ast = vy_ast.parse_to_ast(f"{left} ** {right}") old_node = vyper_ast.body[0].value - new_node = old_node.evaluate() + new_node = old_node.fold() assert contract.foo(left, right) == new_node.value diff --git a/tests/unit/ast/nodes/test_evaluate_boolop.py b/tests/unit/ast/nodes/test_fold_boolop.py similarity index 98% rename from tests/unit/ast/nodes/test_evaluate_boolop.py rename to tests/unit/ast/nodes/test_fold_boolop.py index 8b70537c39..5de4b60bda 100644 --- a/tests/unit/ast/nodes/test_evaluate_boolop.py +++ b/tests/unit/ast/nodes/test_fold_boolop.py @@ -26,7 +26,7 @@ def foo({input_value}) -> bool: vyper_ast = vy_ast.parse_to_ast(literal_op) old_node = vyper_ast.body[0].value - new_node = old_node.evaluate() + new_node = old_node.fold() assert contract.foo(*values) == new_node.value diff --git a/tests/unit/ast/nodes/test_evaluate_compare.py b/tests/unit/ast/nodes/test_fold_compare.py similarity index 95% rename from tests/unit/ast/nodes/test_evaluate_compare.py rename to tests/unit/ast/nodes/test_fold_compare.py index 07f8e70de6..735be43cfd 100644 --- a/tests/unit/ast/nodes/test_evaluate_compare.py +++ b/tests/unit/ast/nodes/test_fold_compare.py @@ -21,7 +21,7 @@ def foo(a: int128, b: int128) -> bool: vyper_ast = vy_ast.parse_to_ast(f"{left} {op} {right}") old_node = vyper_ast.body[0].value - new_node = old_node.evaluate() + new_node = old_node.fold() assert contract.foo(left, right) == new_node.value @@ -41,7 +41,7 @@ def foo(a: uint128, b: uint128) -> bool: vyper_ast = vy_ast.parse_to_ast(f"{left} {op} {right}") old_node = vyper_ast.body[0].value - new_node = old_node.evaluate() + new_node = old_node.fold() assert contract.foo(left, right) == new_node.value @@ -65,7 +65,7 @@ def bar(a: int128) -> bool: vyper_ast = vy_ast.parse_to_ast(f"{left} in {right}") old_node = vyper_ast.body[0].value - new_node = old_node.evaluate() + new_node = old_node.fold() # check runtime == fully folded assert contract.foo(left, right) == new_node.value @@ -94,7 +94,7 @@ def bar(a: int128) -> bool: vyper_ast = vy_ast.parse_to_ast(f"{left} not in {right}") old_node = vyper_ast.body[0].value - new_node = old_node.evaluate() + new_node = old_node.fold() # check runtime == fully folded assert contract.foo(left, right) == new_node.value @@ -109,4 +109,4 @@ def test_compare_type_mismatch(op): vyper_ast = vy_ast.parse_to_ast(f"1 {op} 1.0") old_node = vyper_ast.body[0].value with pytest.raises(UnfoldableNode): - old_node.evaluate() + old_node.fold() diff --git a/tests/unit/ast/nodes/test_evaluate_subscript.py b/tests/unit/ast/nodes/test_fold_subscript.py similarity index 94% rename from tests/unit/ast/nodes/test_evaluate_subscript.py rename to tests/unit/ast/nodes/test_fold_subscript.py index ca50a076a5..59a5725b2c 100644 --- a/tests/unit/ast/nodes/test_evaluate_subscript.py +++ b/tests/unit/ast/nodes/test_fold_subscript.py @@ -21,6 +21,6 @@ def foo(array: int128[10], idx: uint256) -> int128: vyper_ast = vy_ast.parse_to_ast(f"{array}[{idx}]") old_node = vyper_ast.body[0].value - new_node = old_node.evaluate() + new_node = old_node.fold() assert contract.foo(array, idx) == new_node.value diff --git a/tests/unit/ast/nodes/test_evaluate_unaryop.py b/tests/unit/ast/nodes/test_fold_unaryop.py similarity index 96% rename from tests/unit/ast/nodes/test_evaluate_unaryop.py rename to tests/unit/ast/nodes/test_fold_unaryop.py index 63d7a0b7ff..dc447955ed 100644 --- a/tests/unit/ast/nodes/test_evaluate_unaryop.py +++ b/tests/unit/ast/nodes/test_fold_unaryop.py @@ -14,7 +14,7 @@ def foo(a: bool) -> bool: vyper_ast = vy_ast.parse_to_ast(f"not {bool_cond}") old_node = vyper_ast.body[0].value - new_node = old_node.evaluate() + new_node = old_node.fold() assert contract.foo(bool_cond) == new_node.value diff --git a/vyper/ast/README.md b/vyper/ast/README.md index 320c69da0c..9979b60cab 100644 --- a/vyper/ast/README.md +++ b/vyper/ast/README.md @@ -82,8 +82,7 @@ folding include: The process of literal folding includes: -1. Foldable node classes are evaluated via their `evaluate` method, which attempts -to create a new `Constant` from the content of the given node. +1. Foldable node classes are evaluated via their `fold` method, which attempts to create a new `Constant` from the content of the given node. 2. Replacement nodes are generated using the `from_node` class method within the new node class. 3. The modification of the tree is handled by `Module.replace_in_tree`, which locates diff --git a/vyper/ast/folding.py b/vyper/ast/folding.py index 51a58f0bb8..9ad969c733 100644 --- a/vyper/ast/folding.py +++ b/vyper/ast/folding.py @@ -44,7 +44,7 @@ def replace_literal_ops(vyper_module: vy_ast.Module) -> int: node_types = (vy_ast.BoolOp, vy_ast.BinOp, vy_ast.UnaryOp, vy_ast.Compare) for node in vyper_module.get_descendants(node_types, reverse=True): try: - new_node = node.evaluate() + new_node = node.fold() except UnfoldableNode: continue @@ -79,7 +79,7 @@ def replace_subscripts(vyper_module: vy_ast.Module) -> int: for node in vyper_module.get_descendants(vy_ast.Subscript, reverse=True): try: - new_node = node.evaluate() + new_node = node.fold() except UnfoldableNode: continue @@ -114,10 +114,10 @@ def replace_builtin_functions(vyper_module: vy_ast.Module) -> int: name = node.func.id func = DISPATCH_TABLE.get(name) - if func is None or not hasattr(func, "evaluate"): + if func is None or not hasattr(func, "fold"): continue try: - new_node = func.evaluate(node) # type: ignore + new_node = func.fold(node) # type: ignore except UnfoldableNode: continue diff --git a/vyper/ast/nodes.py b/vyper/ast/nodes.py index 0596b0c9e9..dbc2e36014 100644 --- a/vyper/ast/nodes.py +++ b/vyper/ast/nodes.py @@ -366,15 +366,15 @@ def prefold(self) -> Optional["VyperNode"]: """ return None - def evaluate(self) -> "VyperNode": + def fold(self) -> "VyperNode": """ Attempt to evaluate the content of a node and generate a new node from it. - If a node cannot be evaluated it should raise `UnfoldableNode`. This base + If a node cannot be evaluated, it should raise `UnfoldableNode`. This base method acts as a catch-all to raise on any inherited classes that do not implement the method. """ - raise UnfoldableNode(f"{type(self)} cannot be evaluated") + raise UnfoldableNode(f"{type(self)} cannot be folded") def validate(self) -> None: """ @@ -929,7 +929,7 @@ def prefold(self) -> Optional[ExprNode]: return None - def evaluate(self) -> ExprNode: + def fold(self) -> ExprNode: """ Attempt to evaluate the unary operation. @@ -992,7 +992,7 @@ def prefold(self) -> Optional[ExprNode]: value = self.op._op(left.value, right.value) return type(left).from_node(self, value=value) - def evaluate(self) -> ExprNode: + def fold(self) -> ExprNode: """ Attempt to evaluate the arithmetic operation. @@ -1143,7 +1143,7 @@ def prefold(self) -> Optional[ExprNode]: value = self.op._op(values) return NameConstant.from_node(self, value=value) - def evaluate(self) -> ExprNode: + def fold(self) -> ExprNode: """ Attempt to evaluate the boolean operation. @@ -1209,7 +1209,7 @@ def prefold(self) -> Optional[ExprNode]: value = self.op._op(left.value, right.value) return NameConstant.from_node(self, value=value) - def evaluate(self) -> ExprNode: + def fold(self) -> ExprNode: """ Attempt to evaluate the comparison. @@ -1320,7 +1320,7 @@ def prefold(self) -> Optional[ExprNode]: return value.elements[slice_.value] - def evaluate(self) -> ExprNode: + def fold(self) -> ExprNode: """ Attempt to evaluate the subscript. diff --git a/vyper/ast/nodes.pyi b/vyper/ast/nodes.pyi index caa06af918..8fe9600b0b 100644 --- a/vyper/ast/nodes.pyi +++ b/vyper/ast/nodes.pyi @@ -27,7 +27,7 @@ class VyperNode: @classmethod def get_fields(cls: Any) -> set: ... def prefold(self) -> Optional[VyperNode]: ... - def evaluate(self) -> VyperNode: ... + def fold(self) -> VyperNode: ... @classmethod def from_node(cls, node: VyperNode, **kwargs: Any) -> Any: ... def to_dict(self) -> dict: ... diff --git a/vyper/builtins/_signatures.py b/vyper/builtins/_signatures.py index 7fc7716c22..dd428575b2 100644 --- a/vyper/builtins/_signatures.py +++ b/vyper/builtins/_signatures.py @@ -128,11 +128,11 @@ def _validate_arg_types(self, node: vy_ast.Call) -> None: get_exact_type_from_node(arg) def prefold(self, node): - if not hasattr(self, "evaluate"): + if not hasattr(self, "fold"): return None try: - return self.evaluate(node) + return self.fold(node) except (UnfoldableNode, VyperException): return None diff --git a/vyper/builtins/functions.py b/vyper/builtins/functions.py index 5ad9b7cfaa..08f5c6f3bb 100644 --- a/vyper/builtins/functions.py +++ b/vyper/builtins/functions.py @@ -135,7 +135,7 @@ class Floor(BuiltinFunctionT): # TODO: maybe use int136? _return_type = INT256_T - def evaluate(self, node): + def fold(self, node): validate_call_args(node, 1) value = node.args[0]._metadata.get("folded_value") if not isinstance(value, vy_ast.Decimal): @@ -166,7 +166,7 @@ class Ceil(BuiltinFunctionT): # TODO: maybe use int136? _return_type = INT256_T - def evaluate(self, node): + def fold(self, node): validate_call_args(node, 1) value = node.args[0]._metadata.get("folded_value") if not isinstance(value, vy_ast.Decimal): @@ -460,7 +460,7 @@ class Len(BuiltinFunctionT): _inputs = [("b", (StringT.any(), BytesT.any(), DArrayT.any()))] _return_type = UINT256_T - def evaluate(self, node): + def fold(self, node): validate_call_args(node, 1) arg = node.args[0]._metadata.get("folded_value") if isinstance(arg, (vy_ast.Str, vy_ast.Bytes)): @@ -597,7 +597,7 @@ class Keccak256(BuiltinFunctionT): _inputs = [("value", (BytesT.any(), BYTES32_T, StringT.any()))] _return_type = BYTES32_T - def evaluate(self, node): + def fold(self, node): validate_call_args(node, 1) value = node.args[0]._metadata.get("folded_value") if isinstance(value, vy_ast.Bytes): @@ -645,7 +645,7 @@ class Sha256(BuiltinFunctionT): _inputs = [("value", (BYTES32_T, BytesT.any(), StringT.any()))] _return_type = BYTES32_T - def evaluate(self, node): + def fold(self, node): validate_call_args(node, 1) value = node.args[0]._metadata.get("folded_value") if isinstance(value, vy_ast.Bytes): @@ -718,7 +718,7 @@ class MethodID(FoldedFunctionT): _inputs = [("value", StringT.any())] _kwargs = {"output_type": KwargSettings("TYPE_DEFINITION", BytesT(4))} - def evaluate(self, node): + def fold(self, node): validate_call_args(node, 1, ["output_type"]) value = node.args[0]._metadata.get("folded_value") @@ -742,8 +742,8 @@ def fetch_call_return(self, node): return type_ def infer_arg_types(self, node, expected_return_typ=None): - # call `evaluate` for its typechecking side effects - self.evaluate(node) + # call `fold` for its typechecking side effects + self.fold(node) return [self._inputs[0][1]] def infer_kwarg_types(self, node): @@ -993,7 +993,7 @@ def get_denomination(self, node): return denom - def evaluate(self, node): + def fold(self, node): validate_call_args(node, 2) denom = self.get_denomination(node) @@ -1012,9 +1012,9 @@ def fetch_call_return(self, node): return self._return_type def infer_arg_types(self, node, expected_return_typ=None): - # call `evaluate` for its typechecking side effects` + # call `fold` for its typechecking side effects` try: - self.evaluate(node) + self.fold(node) except UnfoldableNode: pass @@ -1350,7 +1350,7 @@ class BitwiseAnd(BuiltinFunctionT): _return_type = UINT256_T _warned = False - def evaluate(self, node): + def fold(self, node): if not self.__class__._warned: vyper_warn("`bitwise_and()` is deprecated! Please use the & operator instead.") self.__class__._warned = True @@ -1375,7 +1375,7 @@ class BitwiseOr(BuiltinFunctionT): _return_type = UINT256_T _warned = False - def evaluate(self, node): + def fold(self, node): if not self.__class__._warned: vyper_warn("`bitwise_or()` is deprecated! Please use the | operator instead.") self.__class__._warned = True @@ -1400,7 +1400,7 @@ class BitwiseXor(BuiltinFunctionT): _return_type = UINT256_T _warned = False - def evaluate(self, node): + def fold(self, node): if not self.__class__._warned: vyper_warn("`bitwise_xor()` is deprecated! Please use the ^ operator instead.") self.__class__._warned = True @@ -1425,7 +1425,7 @@ class BitwiseNot(BuiltinFunctionT): _return_type = UINT256_T _warned = False - def evaluate(self, node): + def fold(self, node): if not self.__class__._warned: vyper_warn("`bitwise_not()` is deprecated! Please use the ~ operator instead.") self.__class__._warned = True @@ -1451,7 +1451,7 @@ class Shift(BuiltinFunctionT): _return_type = UINT256_T _warned = False - def evaluate(self, node): + def fold(self, node): if not self.__class__._warned: vyper_warn("`shift()` is deprecated! Please use the << or >> operator instead.") self.__class__._warned = True @@ -1502,7 +1502,7 @@ class _AddMulMod(BuiltinFunctionT): _inputs = [("a", UINT256_T), ("b", UINT256_T), ("c", UINT256_T)] _return_type = UINT256_T - def evaluate(self, node): + def fold(self, node): validate_call_args(node, 3) args = [i._metadata.get("folded_value") for i in node.args] if isinstance(args[2], vy_ast.Int) and args[2].value == 0: @@ -1543,7 +1543,7 @@ class PowMod256(BuiltinFunctionT): _inputs = [("a", UINT256_T), ("b", UINT256_T)] _return_type = UINT256_T - def evaluate(self, node): + def fold(self, node): validate_call_args(node, 2) values = [i._metadata.get("folded_value") for i in node.args] if any(not isinstance(i, vy_ast.Int) for i in values): @@ -1564,7 +1564,7 @@ class Abs(BuiltinFunctionT): _inputs = [("value", INT256_T)] _return_type = INT256_T - def evaluate(self, node): + def fold(self, node): validate_call_args(node, 1) value = node.args[0]._metadata.get("folded_value") if not isinstance(value, vy_ast.Int): @@ -2003,7 +2003,7 @@ class UnsafeDiv(_UnsafeMath): class _MinMax(BuiltinFunctionT): _inputs = [("a", (DecimalT(), IntegerT.any())), ("b", (DecimalT(), IntegerT.any()))] - def evaluate(self, node): + def fold(self, node): validate_call_args(node, 2) left = node.args[0]._metadata.get("folded_value") @@ -2081,7 +2081,7 @@ def fetch_call_return(self, node): len_needed = math.ceil(bits * math.log(2) / math.log(10)) return StringT(len_needed) - def evaluate(self, node): + def fold(self, node): validate_call_args(node, 1) value = node.args[0]._metadata.get("folded_value") if not isinstance(value, vy_ast.Int): @@ -2569,7 +2569,7 @@ def build_IR(self, expr, args, kwargs, context): class _MinMaxValue(TypenameFoldedFunctionT): - def evaluate(self, node): + def fold(self, node): self._validate_arg_types(node) input_type = type_from_annotation(node.args[0]) @@ -2588,8 +2588,8 @@ def evaluate(self, node): return ret def infer_arg_types(self, node, expected_return_typ=None): - # call `evaluate` for its typechecking side effects - self.evaluate(node) + # call `fold` for its typechecking side effects + self.fold(node) input_typedef = TYPE_T(type_from_annotation(node.args[0])) return [input_typedef] @@ -2611,7 +2611,7 @@ def _eval(self, type_): class Epsilon(TypenameFoldedFunctionT): _id = "epsilon" - def evaluate(self, node): + def fold(self, node): self._validate_arg_types(node) input_type = type_from_annotation(node.args[0])