示例#1
0
def ceil(viper_ast: ViperAST, dec, scaling_factor: int, pos=None, info=None):
    mceil = mangled.MATH_CEIL
    domain = mangled.MATH_DOMAIN
    scaling_factor_lit = viper_ast.IntLit(scaling_factor, pos)
    args = [dec, scaling_factor_lit]
    return viper_ast.DomainFuncApp(mceil, args, viper_ast.Int, pos, info,
                                   domain)
示例#2
0
def method_id(viper_ast: ViperAST, method, len: int, pos=None, info=None):
    mid = mangled.BLOCKCHAIN_METHOD_ID
    domain = mangled.BLOCKCHAIN_DOMAIN
    rl = viper_ast.IntLit(len, pos)
    return viper_ast.DomainFuncApp(mid, [method, rl],
                                   viper_ast.SeqType(viper_ast.Int), pos, info,
                                   domain)
示例#3
0
def map_sum(viper_ast: ViperAST, ref, key_type, pos=None, info=None):
    type_vars = {viper_ast.TypeVar(mangled.MAP_KEY_VAR): key_type}
    type = viper_ast.Int
    msum = mangled.MAP_SUM
    domain = mangled.MAP_INT_DOMAIN
    return viper_ast.DomainFuncApp(msum, [ref], type, pos, info, domain,
                                   type_vars)
示例#4
0
def floor(viper_ast: ViperAST, dec, scaling_factor: int, pos=None, info=None):
    mfloor = mangled.MATH_FLOOR
    domain = mangled.MATH_DOMAIN
    scaling_factor_lit = viper_ast.IntLit(scaling_factor, pos)
    args = [dec, scaling_factor_lit]
    return viper_ast.DomainFuncApp(mfloor, args, viper_ast.Int, pos, info,
                                   domain)
示例#5
0
def convert_unsigned_int_to_bytes32(viper_ast: ViperAST,
                                    i,
                                    pos=None,
                                    info=None):
    domain = mangled.CONVERT_DOMAIN
    function = mangled.CONVERT_UNSIGNED_INT_TO_BYTES32
    return viper_ast.DomainFuncApp(function, [i],
                                   viper_ast.SeqType(viper_ast.Int), pos, info,
                                   domain)
示例#6
0
def mod(viper_ast: ViperAST, dividend, divisor, pos=None, info=None):
    # We need a special mod function because Vyper uses truncating division
    # instead of Viper's floor division
    mmod = mangled.MATH_MOD
    domain = mangled.MATH_DOMAIN
    # We pass the Viper floor division as a third argument to trigger a correct
    # division by 0 error instead of an assertion failure if divisor == 0
    args = [dividend, divisor, viper_ast.Mod(dividend, divisor, pos)]
    return viper_ast.DomainFuncApp(mmod, args, viper_ast.Int, pos, info,
                                   domain)
示例#7
0
def pad32(viper_ast: ViperAST, bytes, pos=None, info=None):
    """
    Left-pads a byte array shorter than 32 bytes with 0s so that its resulting length is 32.
    Left-crops a byte array longer than 32 bytes so that its resulting length is 32.
    """
    domain = mangled.CONVERT_DOMAIN
    function = mangled.CONVERT_PAD32
    return viper_ast.DomainFuncApp(function, [bytes],
                                   viper_ast.SeqType(viper_ast.Int), pos, info,
                                   domain)
示例#8
0
def havoc_var(viper_ast: ViperAST, viper_type, ctx: Context):
    if ctx.is_pure_function:
        pure_idx = ctx.next_pure_var_index()
        function_result = viper_ast.Result(struct_type(viper_ast))
        return struct_get_idx(viper_ast, function_result, pure_idx, viper_type)
    else:
        havoc_name = ctx.new_local_var_name('havoc')
        havoc = viper_ast.LocalVarDecl(havoc_name, viper_type)
        ctx.new_local_vars.append(havoc)
        return havoc.localVar()
示例#9
0
def set_lock(viper_ast: ViperAST,
             name: str,
             val: bool,
             ctx: Context,
             pos=None,
             info=None):
    lock_name = mangled.lock_name(name)
    value = viper_ast.TrueLit(pos) if val else viper_ast.FalseLit(pos)
    self_var = ctx.self_var.local_var(ctx)
    return struct_set(viper_ast, self_var, value, lock_name, viper_ast.Bool,
                      ctx.self_type, pos, info)
示例#10
0
def struct_type_tag(viper_ast: ViperAST, ref, pos=None, info=None):
    """
    Returns the type tag of a struct which we store at index -1 of a struct
    """
    domain = mangled.STRUCT_OPS_DOMAIN
    idx = viper_ast.IntLit(mangled.STRUCT_TYPE_LOC)
    field = struct_loc(viper_ast, ref, idx, pos)
    getter = mangled.STRUCT_GET
    type_type = viper_ast.Int
    type_map = _struct_type_var_map(viper_ast, type_type)
    return viper_ast.DomainFuncApp(getter, [field], type_type, pos, info,
                                   domain, type_map)
示例#11
0
def implements(viper_ast: ViperAST,
               address,
               interface: str,
               ctx: Context,
               pos=None,
               info=None):
    impl = mangled.IMPLEMENTS
    domain = mangled.CONTRACT_DOMAIN
    intf = viper_ast.IntLit(
        first_index(lambda i: i == interface, ctx.program.interfaces), pos)
    return viper_ast.DomainFuncApp(impl, [address, intf], viper_ast.Bool, pos,
                                   info, domain)
示例#12
0
def struct_get_idx(viper_ast: ViperAST,
                   ref,
                   idx: Union[int, Expr],
                   viper_type,
                   pos=None,
                   info=None):
    domain = mangled.STRUCT_OPS_DOMAIN
    idx_lit = viper_ast.IntLit(idx) if isinstance(idx, int) else idx
    field = struct_loc(viper_ast, ref, idx_lit, pos, info)
    getter = mangled.STRUCT_GET
    type_map = _struct_type_var_map(viper_ast, viper_type)
    return viper_ast.DomainFuncApp(getter, [field], viper_type, pos, info,
                                   domain, type_map)
示例#13
0
def array_init(viper_ast: ViperAST,
               arg,
               size: int,
               element_type,
               pos=None,
               info=None):
    arr_type = array_type(viper_ast, element_type)
    type_vars = {viper_ast.TypeVar(mangled.ARRAY_ELEMENT_VAR): element_type}
    size = viper_ast.IntLit(size, pos, info)
    init = mangled.ARRAY_INIT
    domain = mangled.ARRAY_DOMAIN
    return viper_ast.DomainFuncApp(init, [arg, size], arr_type, pos, info,
                                   domain, type_vars)
示例#14
0
def struct_set_idx(viper_ast: ViperAST,
                   ref,
                   val,
                   idx: int,
                   member_type,
                   pos=None,
                   info=None):
    setter = mangled.STRUCT_SET
    s_type = struct_type(viper_ast)
    domain = mangled.STRUCT_OPS_DOMAIN
    idx = viper_ast.IntLit(idx)
    type_map = _struct_type_var_map(viper_ast, member_type)
    return viper_ast.DomainFuncApp(setter, [ref, idx, val], s_type, pos, info,
                                   domain, type_map)
示例#15
0
def struct_get(viper_ast: ViperAST,
               ref,
               member: str,
               member_type,
               struct_type: StructType,
               pos=None,
               info=None):
    domain = mangled.STRUCT_OPS_DOMAIN
    idx = viper_ast.IntLit(struct_type.member_indices[member])
    field = struct_loc(viper_ast, ref, idx, pos, info)
    getter = mangled.STRUCT_GET
    type_map = _struct_type_var_map(viper_ast, member_type)
    return viper_ast.DomainFuncApp(getter, [field], member_type, pos, info,
                                   domain, type_map)
示例#16
0
def struct_set(viper_ast: ViperAST,
               ref,
               val,
               member: str,
               member_type,
               type: StructType,
               pos=None,
               info=None):
    setter = mangled.STRUCT_SET
    s_type = struct_type(viper_ast)
    domain = mangled.STRUCT_OPS_DOMAIN
    idx = viper_ast.IntLit(type.member_indices[member])
    type_map = _struct_type_var_map(viper_ast, member_type)
    return viper_ast.DomainFuncApp(setter, [ref, idx, val], s_type, pos, info,
                                   domain, type_map)
示例#17
0
def array_get(viper_ast: ViperAST,
              ref,
              idx,
              element_type,
              pos=None,
              info=None):
    return viper_ast.SeqIndex(ref, idx, pos, info)
示例#18
0
def w_unwrap(viper_ast: ViperAST, value, pos=None, info=None):
    if isinstance(viper_ast, WrappedViperAST):
        viper_ast = viper_ast.viper_ast
    wi_unwrap = mangled.WRAPPED_INT_UNWRAP
    domain = mangled.WRAPPED_INT_DOMAIN
    return viper_ast.DomainFuncApp(wi_unwrap, [value], viper_ast.Int, pos,
                                   info, domain)
示例#19
0
def w_mul(viper_ast: ViperAST, first, second, pos=None, info=None):
    if isinstance(viper_ast, WrappedViperAST):
        viper_ast = viper_ast.viper_ast
    wi_mul = mangled.WRAPPED_INT_MUL
    domain = mangled.WRAPPED_INT_DOMAIN
    args = [first, second]
    return viper_ast.DomainFuncApp(wi_mul, args, wrapped_int_type(viper_ast),
                                   pos, info, domain)
示例#20
0
def convert_bytes32_to_unsigned_int(viper_ast: ViperAST,
                                    bytes,
                                    pos=None,
                                    info=None):
    domain = mangled.CONVERT_DOMAIN
    function = mangled.CONVERT_BYTES32_TO_UNSIGNED_INT
    return viper_ast.DomainFuncApp(function, [bytes], viper_ast.Int, pos, info,
                                   domain)
示例#21
0
def array_set(viper_ast: ViperAST,
              ref,
              idx,
              value,
              element_type,
              pos=None,
              info=None):
    return viper_ast.SeqUpdate(ref, idx, value, pos, info)
示例#22
0
def struct_init(viper_ast: ViperAST,
                args,
                struct: StructType,
                pos=None,
                info=None):
    domain = mangled.struct_name(struct.name, struct.kind)
    init_name = mangled.struct_init_name(struct.name, struct.kind)
    type = struct_type(viper_ast)
    return viper_ast.DomainFuncApp(init_name, args, type, pos, info, domain)
示例#23
0
def struct_eq(viper_ast: ViperAST,
              left,
              right,
              struct: StructType,
              pos=None,
              info=None):
    domain = mangled.struct_name(struct.name, struct.kind)
    eq = mangled.struct_eq_name(struct.name, struct.kind)
    return viper_ast.DomainFuncApp(eq, [left, right], viper_ast.Bool, pos,
                                   info, domain)
示例#24
0
def w_mod(viper_ast: ViperAST, first, second, pos=None, info=None):
    if isinstance(viper_ast, WrappedViperAST):
        viper_ast = viper_ast.viper_ast
    wi_mod = mangled.WRAPPED_INT_MOD
    domain = mangled.WRAPPED_INT_DOMAIN
    args = [first, second]
    func_app = viper_ast.DomainFuncApp(wi_mod, args,
                                       wrapped_int_type(viper_ast), pos, info,
                                       domain)
    is_div_zero = viper_ast.EqCmp(viper_ast.IntLit(0),
                                  w_unwrap(viper_ast, second, pos, info), pos,
                                  info)
    artificial_div_zero = w_wrap(
        viper_ast,
        viper_ast.Mod(w_unwrap(viper_ast, first, pos, info),
                      w_unwrap(viper_ast, second, pos, info), pos, info), pos,
        info)
    return viper_ast.CondExp(is_div_zero, artificial_div_zero, func_app, pos,
                             info)
示例#25
0
def offer_predicate(viper_ast: ViperAST,
                    from_resource,
                    to_resource,
                    from_val,
                    to_val,
                    from_addr,
                    to_addr,
                    pos=None):
    return viper_ast.PredicateAccess(
        [from_resource, to_resource, from_val, to_val, from_addr, to_addr],
        mangled.OFFER, pos)
示例#26
0
def ghost_function(viper_ast: ViperAST,
                   function,
                   address,
                   struct,
                   args,
                   return_type,
                   pos=None,
                   info=None):
    ghost_func = mangled.ghost_function_name(function.interface, function.name)
    return viper_ast.FuncApp(ghost_func, [address, struct, *args], pos, info,
                             return_type)
示例#27
0
def map_eq(viper_ast: ViperAST,
           left,
           right,
           key_type,
           value_type,
           pos=None,
           info=None):
    type_vars = _map_type_var_map(viper_ast, key_type, value_type)
    eq = mangled.MAP_EQ
    domain = mangled.MAP_DOMAIN
    return viper_ast.DomainFuncApp(eq, [left, right], viper_ast.Bool, pos,
                                   info, domain, type_vars)
示例#28
0
def map_init(viper_ast: ViperAST,
             arg,
             key_type,
             value_type,
             pos=None,
             info=None):
    mp_type = map_type(viper_ast, key_type, value_type)
    type_vars = _map_type_var_map(viper_ast, key_type, value_type)
    init = mangled.MAP_INIT
    domain = mangled.MAP_DOMAIN
    return viper_ast.DomainFuncApp(init, [arg], mp_type, pos, info, domain,
                                   type_vars)
示例#29
0
def map_get(viper_ast: ViperAST,
            ref,
            idx,
            key_type,
            value_type,
            pos=None,
            info=None):
    type_vars = _map_type_var_map(viper_ast, key_type, value_type)
    get = mangled.MAP_GET
    domain = mangled.MAP_DOMAIN
    return viper_ast.DomainFuncApp(get, [ref, idx], value_type, pos, info,
                                   domain, type_vars)
示例#30
0
def map_set(viper_ast: ViperAST,
            ref,
            idx,
            value,
            key_type,
            value_type,
            pos=None,
            info=None):
    type_vars = _map_type_var_map(viper_ast, key_type, value_type)
    mtype = map_type(viper_ast, key_type, value_type)
    mset = mangled.MAP_SET
    domain = mangled.MAP_DOMAIN
    return viper_ast.DomainFuncApp(mset, [ref, idx, value], mtype, pos, info,
                                   domain, type_vars)