Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: shift operators #3019

Merged
merged 19 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions vyper/ast/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,20 @@ class BitXor(VyperNode):
_op = operator.xor


class LShift(VyperNode):
__slots__ = ()
_description = "bitwise left shift"
_pretty = "<<"
_op = operator.lshift


class RShift(VyperNode):
__slots__ = ()
_description = "bitwise right shift"
_pretty = ">>"
_op = operator.rshift


class BoolOp(VyperNode):
__slots__ = ("op", "values")

Expand Down
2 changes: 2 additions & 0 deletions vyper/ast/nodes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ class Mult(VyperNode): ...
class Div(VyperNode): ...
class Mod(VyperNode): ...
class Pow(VyperNode): ...
class LShift(VyperNode): ...
class RShift(VyperNode): ...
class BoolOp(VyperNode): ...
class And(VyperNode): ...
class Or(VyperNode): ...
Expand Down
5 changes: 5 additions & 0 deletions vyper/builtin_functions/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1441,8 +1441,13 @@ class Shift(BuiltinFunction):
("x", (Uint256Definition(), Int256Definition())),
("shift_bits", SignedIntegerAbstractType()),
]
_warned = False

def evaluate(self, node):
if not self.__class__._warned:
vyper_warn("`shift()` is deprecated! Please use the << or >> operator instead.")
self.__class__._warned = True

validate_call_args(node, 2)
if [i for i in node.args if not isinstance(i, vy_ast.Num)]:
raise UnfoldableNode
Expand Down
12 changes: 12 additions & 0 deletions vyper/codegen/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
getpos,
make_setter,
pop_dyn_array,
sar,
shl,
shr,
unwrap_location,
)
from vyper.codegen.ir_node import IRnode
Expand Down Expand Up @@ -368,6 +371,15 @@ def parse_BinOp(self):
new_typ = left.typ
return IRnode.from_list(["xor", left, right], typ=new_typ)

if isinstance(self.expr.op, vy_ast.LShift):
new_typ = left.typ
return IRnode.from_list(shl(right, left), typ=new_typ)
if isinstance(self.expr.op, vy_ast.RShift):
new_typ = left.typ
op = shr if not left.typ._int_info.is_signed else sar
# note: sar NotImplementedError for pre-constantinople
return IRnode.from_list(op(right, left), typ=new_typ)

out_typ = BaseType(ltyp)

with left.cache_when_complex("x") as (b1, x), right.cache_when_complex("y") as (b2, y):
Expand Down
5 changes: 5 additions & 0 deletions vyper/semantics/types/value/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ def validate_numeric_op(
if self._invalid_op and isinstance(node.op, self._invalid_op):
raise InvalidOperation(f"Cannot perform {node.op.description} on {self}", node)

if isinstance(node.op, (vy_ast.LShift, vy_ast.RShift)) and self._bits != 256:
raise InvalidOperation(
f"Cannot perform {node.op.description} on non-int256/uint256 type!", node
)

if isinstance(node.op, vy_ast.Pow):
if isinstance(node, vy_ast.BinOp):
left, right = node.left, node.right
Expand Down