Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Handle different numbers of inputs
count = len(self.expr.values)
if count < 2:
raise StructureException("Expected at least two arguments for a bool op", self.expr)
elif count == 2:
left = Expr.parse_value_expr(self.expr.values[0], self.context)
right = Expr.parse_value_expr(self.expr.values[1], self.context)
return LLLnode.from_list([op, left, right], typ='bool', pos=getpos(self.expr))
else:
left = Expr.parse_value_expr(self.expr.values[0], self.context)
right = Expr.parse_value_expr(self.expr.values[1], self.context)
p = ['seq', [op, left, right]]
values = self.expr.values[2:]
while len(values) > 0:
value = Expr.parse_value_expr(values[0], self.context)
p = [op, value, p]
values = values[1:]
return LLLnode.from_list(p, typ='bool', pos=getpos(self.expr))
def unroll_constant(self, const, global_ctx):
ann_expr = None
expr = Expr.parse_value_expr(
const.value,
Context(
vars=None,
global_ctx=global_ctx,
origcode=const.source_code,
memory_allocator=MemoryAllocator()
),
)
annotation_type = global_ctx.parse_type(const.annotation.args[0], None)
fail = False
if is_instances([expr.typ, annotation_type], ByteArrayType):
if expr.typ.maxlen < annotation_type.maxlen:
return const
fail = True
def call_lookup_specs(stmt_expr, context):
from vyper.parser.expr import Expr
method_name = stmt_expr.func.attr
if len(stmt_expr.keywords):
raise TypeMismatchException(
"Cannot use keyword arguments in calls to functions via 'self'",
stmt_expr,
)
expr_args = [
Expr(arg, context).lll_node
for arg in stmt_expr.args
]
sig = FunctionSignature.lookup_sig(
context.sigs,
method_name,
expr_args,
stmt_expr,
context,
)
return method_name, expr_args, sig
maxlen += (typ.count - 1) * 32
typ = typ.subtype
def check_list_type_match(provided): # Check list types match.
if provided != typ:
raise TypeMismatchException(
"Log list type '%s' does not match provided, expected '%s'" % (provided, typ)
)
# List from storage
if isinstance(arg, ast.Attribute) and arg.value.id == 'self':
stor_list = context.globals[arg.attr]
check_list_type_match(stor_list.typ.subtype)
size = stor_list.typ.count
for offset in range(0, size):
arg2 = LLLnode.from_list(['sload', ['add', ['sha3_32', Expr(arg, context).lll_node], offset]],
typ=typ)
p_holder = context.new_placeholder(BaseType(32)) if offset > 0 else placeholder
holder, maxlen = pack_args_by_32(holder, maxlen, arg2, typ, context, p_holder, pos=pos)
# List from variable.
elif isinstance(arg, ast.Name):
size = context.vars[arg.id].size
pos = context.vars[arg.id].pos
check_list_type_match(context.vars[arg.id].typ.subtype)
for i in range(0, size):
offset = 32 * i
arg2 = LLLnode.from_list(pos + offset, typ=typ, location='memory')
p_holder = context.new_placeholder(BaseType(32)) if i > 0 else placeholder
holder, maxlen = pack_args_by_32(holder, maxlen, arg2, typ, context, p_holder, pos=pos)
# is list literal.
else:
holder, maxlen = pack_args_by_32(holder, maxlen, arg.elts[0], typ, context, placeholder, pos=pos)
def build_in_comparator(self):
left = Expr(self.expr.left, self.context).lll_node
right = Expr(self.expr.comparators[0], self.context).lll_node
if left.typ != right.typ.subtype:
raise TypeMismatchException(
f"{left.typ} cannot be in a list of {right.typ.subtype}",
)
result_placeholder = self.context.new_placeholder(BaseType('bool'))
setter = []
# Load nth item from list in memory.
if right.value == 'multi':
# Copy literal to memory to be compared.
tmp_list = LLLnode.from_list(
obj=self.context.new_placeholder(ListType(right.typ.subtype, right.typ.count)),
typ=ListType(right.typ.subtype, right.typ.count),
pos=getpos(code),
))
# Function has default arguments.
if sig.total_default_args > 0: # Function with default parameters.
default_sigs = sig_utils.generate_default_arg_sigs(code, context.sigs, context.global_ctx)
sig_chain: List[Any] = ['seq']
for default_sig in default_sigs:
sig_compare, private_label = get_sig_statements(default_sig, getpos(code))
# Populate unset default variables
set_defaults = []
for arg_name in get_default_names_to_set(sig, default_sig):
value = Expr(sig.default_values[arg_name], context).lll_node
var = context.vars[arg_name]
left = LLLnode.from_list(var.pos, typ=var.typ, location='memory',
pos=getpos(code), mutable=var.mutable)
set_defaults.append(make_setter(left, value, 'memory', pos=getpos(code)))
current_sig_arg_names = [x.name for x in default_sig.args]
# Load all variables in default section, if private,
# because the stack is a linear pipe.
copier_arg_count = len(default_sig.args)
copier_arg_names = current_sig_arg_names
# Order copier_arg_names, this is very important.
copier_arg_names = [
x.name
for x in default_sig.args
if x.name in copier_arg_names
list_name = target.value.id
raise_exception = True
if raise_exception:
raise StructureException(
f"Altering list '{list_name}' which is being iterated!",
self.stmt,
)
if isinstance(target, ast.Name) and target.id in self.context.forvars:
raise StructureException(
f"Altering iterator '{target.id}' which is in use!",
self.stmt,
)
if isinstance(target, ast.Tuple):
return Expr(target, self.context).lll_node
target = Expr.parse_variable_location(target, self.context)
if target.location == 'storage' and self.context.is_constant():
raise ConstancyViolationException(
f"Cannot modify storage inside {self.context.pp_constancy()}: {target.annotation}",
self.stmt,
)
if not target.mutable:
raise ConstancyViolationException(
f"Cannot modify function argument: {target.annotation}",
self.stmt,
)
return target
def _is_valid_contract_assign(self):
if self.expr.args and len(self.expr.args) == 1:
arg_lll = Expr(self.expr.args[0], self.context).lll_node
if arg_lll.typ == BaseType('address'):
return True, arg_lll
return False, None
def concat(expr, context):
args = [Expr(arg, context).lll_node for arg in expr.args]
if len(args) < 2:
raise StructureException("Concat expects at least two arguments", expr)
prev_type = ''
for _, (expr_arg, arg) in enumerate(zip(expr.args, args)):
if not isinstance(arg.typ, ByteArrayLike) and not is_base_type(arg.typ, 'bytes32'):
raise TypeMismatchException("Concat expects string, bytes or bytes32 objects", expr_arg)
current_type = (
'bytes'
if isinstance(arg.typ, ByteArrayType) or is_base_type(arg.typ, 'bytes32')
else 'string'
)
if prev_type and current_type != prev_type:
raise TypeMismatchException(
(
def parse_for_list(self):
with self.context.range_scope():
iter_list_node = Expr(self.stmt.iter, self.context).lll_node
if not isinstance(iter_list_node.typ.subtype, BaseType): # Sanity check on list subtype.
raise StructureException('For loops allowed only on basetype lists.', self.stmt.iter)
iter_var_type = (
self.context.vars.get(self.stmt.iter.id).typ
if isinstance(self.stmt.iter, ast.Name)
else None
)
subtype = iter_list_node.typ.subtype.typ
varname = self.stmt.target.id
value_pos = self.context.new_variable(
varname,
BaseType(subtype, unit=iter_list_node.typ.subtype.unit),
)
i_pos_raw_name = '_index_for_' + varname
i_pos = self.context.new_internal_variable(
i_pos_raw_name,