Skip to content

Commit 27e8e66

Browse files
Merge pull request #2300 from iamdefinitelyahuman/refactor-base-type-conversion
Remove base_type_conversion
2 parents bc7fb85 + 9cda063 commit 27e8e66

4 files changed

Lines changed: 14 additions & 80 deletions

File tree

tests/parser/functions/test_empty.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ def foo():
8989
9090
assert self.foobar == ZERO_ADDRESS
9191
assert bar == ZERO_ADDRESS
92+
""",
93+
"""
94+
@external
95+
def foo() -> bool:
96+
return empty(bool)
9297
""",
9398
],
9499
)
@@ -233,11 +238,6 @@ def foo() -> uint256:
233238
""",
234239
"""
235240
@external
236-
def foo() -> bool:
237-
return empty(bool)
238-
""",
239-
"""
240-
@external
241241
def foo() -> decimal:
242242
return empty(1.0)
243243
""",

vyper/parser/events.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from vyper.parser.expr import Expr
77
from vyper.parser.lll_node import LLLnode
88
from vyper.parser.parser_utils import (
9-
base_type_conversion,
109
getpos,
1110
make_byte_array_copier,
1211
make_setter,
@@ -99,7 +98,7 @@ def pack_args_by_32(
9998
value = unwrap_location(arg)
10099
else:
101100
value = Expr(arg, context).lll_node
102-
value = base_type_conversion(value, value.typ, value.typ, pos)
101+
value = unwrap_location(value)
103102
holder.append(LLLnode.from_list(["mstore", placeholder, value], location="memory"))
104103
elif isinstance(typ, ArrayValueAbstractType):
105104

vyper/parser/parser_utils.py

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,7 @@
2424
has_dynamic_data,
2525
is_base_type,
2626
)
27-
from vyper.types.types import InterfaceType
28-
from vyper.utils import (
29-
DECIMAL_DIVISOR,
30-
GAS_IDENTITY,
31-
GAS_IDENTITYWORD,
32-
MemoryPositions,
33-
SizeLimits,
34-
)
27+
from vyper.utils import GAS_IDENTITY, GAS_IDENTITYWORD, MemoryPositions
3528

3629
getcontext().prec = 78 # MAX_UINT256 < 1e78
3730

@@ -342,7 +335,7 @@ def add_variable_offset(parent, key, pos, array_bounds_check=True):
342335
sub = LLLnode.from_list(["sha3", ["add", value, 32], key])
343336
else:
344337
subtype = typ.valuetype
345-
sub = base_type_conversion(key, key.typ, typ.keytype, pos=pos)
338+
sub = unwrap_location(key)
346339

347340
if sub is not None and location == "storage":
348341
return LLLnode.from_list(["sha3_64", parent, sub], typ=subtype, location="storage")
@@ -378,42 +371,6 @@ def add_variable_offset(parent, key, pos, array_bounds_check=True):
378371
)
379372

380373

381-
# Convert from one base type to another
382-
@type_check_wrapper
383-
def base_type_conversion(orig, frm, to, pos, in_function_call=False):
384-
orig = unwrap_location(orig)
385-
386-
# do the base type check so we can use BaseType attributes
387-
if not isinstance(frm, BaseType) or not isinstance(to, BaseType):
388-
return
389-
390-
if getattr(frm, "is_literal", False):
391-
for typ in (frm.typ, to.typ):
392-
if typ in ("int128", "uint256") and not SizeLimits.in_bounds(typ, orig.value):
393-
return
394-
395-
is_decimal_int128_conversion = frm.typ == "int128" and to.typ == "decimal"
396-
is_same_type = frm.typ == to.typ
397-
is_literal_conversion = frm.is_literal and (frm.typ, to.typ) == ("int128", "uint256")
398-
is_address_conversion = isinstance(frm, InterfaceType) and to.typ == "address"
399-
if not (
400-
is_same_type
401-
or is_literal_conversion
402-
or is_address_conversion
403-
or is_decimal_int128_conversion
404-
):
405-
return
406-
407-
# handle None value inserted by `empty()`
408-
if orig.value is None:
409-
return LLLnode.from_list(0, typ=to)
410-
411-
if is_decimal_int128_conversion:
412-
return LLLnode.from_list(["mul", orig, DECIMAL_DIVISOR], typ=BaseType("decimal"),)
413-
414-
return LLLnode(orig.value, orig.args, typ=to, add_gas_estimate=orig.add_gas_estimate)
415-
416-
417374
# Unwrap location
418375
def unwrap_location(orig):
419376
if orig.location == "memory":
@@ -423,6 +380,9 @@ def unwrap_location(orig):
423380
elif orig.location == "calldata":
424381
return LLLnode.from_list(["calldataload", orig], typ=orig.typ)
425382
else:
383+
# handle None value inserted by `empty`
384+
if orig.value is None:
385+
return LLLnode.from_list(0, typ=orig.typ)
426386
return orig
427387

428388

@@ -548,9 +508,7 @@ def _make_array_index_setter(target, target_token, pos, location, offset):
548508
def make_setter(left, right, location, pos, in_function_call=False):
549509
# Basic types
550510
if isinstance(left.typ, BaseType):
551-
right = base_type_conversion(
552-
right, right.typ, left.typ, pos, in_function_call=in_function_call,
553-
)
511+
right = unwrap_location(right)
554512
if location == "storage":
555513
return LLLnode.from_list(["sstore", left, right], typ=None)
556514
elif location == "memory":

vyper/parser/stmt.py

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from vyper.parser.expr import Expr
99
from vyper.parser.parser_utils import (
1010
LLLnode,
11-
base_type_conversion,
1211
getpos,
1312
make_byte_array_copier,
1413
make_setter,
@@ -377,18 +376,7 @@ def parse_AugAssign(self):
377376
self.context,
378377
)
379378
return LLLnode.from_list(
380-
[
381-
"with",
382-
"_stloc",
383-
target,
384-
[
385-
"sstore",
386-
"_stloc",
387-
base_type_conversion(
388-
lll_node, lll_node.typ, target.typ, pos=getpos(self.stmt)
389-
),
390-
],
391-
],
379+
["with", "_stloc", target, ["sstore", "_stloc", unwrap_location(lll_node)]],
392380
typ=None,
393381
pos=getpos(self.stmt),
394382
)
@@ -406,18 +394,7 @@ def parse_AugAssign(self):
406394
self.context,
407395
)
408396
return LLLnode.from_list(
409-
[
410-
"with",
411-
"_mloc",
412-
target,
413-
[
414-
"mstore",
415-
"_mloc",
416-
base_type_conversion(
417-
lll_node, lll_node.typ, target.typ, pos=getpos(self.stmt)
418-
),
419-
],
420-
],
397+
["with", "_mloc", target, ["mstore", "_mloc", unwrap_location(lll_node)]],
421398
typ=None,
422399
pos=getpos(self.stmt),
423400
)

0 commit comments

Comments
 (0)