Ejemplo n.º 1
0
 def generic(self, args, kws):
     # Resolution of members for record and structured arrays
     record, idx, value = args
     if isinstance(record, types.Record) and isinstance(idx, str):
         expectedty = record.typeof(idx)
         if self.context.can_convert(value, expectedty) is not None:
             return signature(types.void, record, types.literal(idx), value)
Ejemplo n.º 2
0
 def generic(self, args, kws):
     # Resolution of members for record and structured arrays
     record, idx, value = args
     if isinstance(record, types.Record) and isinstance(idx, str):
         expectedty = record.typeof(idx)
         if self.context.can_convert(value, expectedty) is not None:
             return signature(types.void, record, types.literal(idx), value)
Ejemplo n.º 3
0
def lower_setattr(typingctx, inst_type, attr_type, val_type):
    if (isinstance(attr_type, types.Literal)
            and isinstance(inst_type, types.StructRef)):

        attr = attr_type.literal_value

        def codegen(context, builder, sig, args):
            [instance, attr_v, val] = args

            utils = _Utils(context, builder, inst_type)
            dataval = utils.get_data_struct(instance)
            # cast val to the correct type
            field_type = inst_type.field_dict[attr]
            casted = context.cast(builder, val, val_type, field_type)

            # read old
            old_value = getattr(dataval, attr)
            # incref new value
            context.nrt.incref(builder, val_type, casted)
            # decref old value (must be last in case new value is old value)
            context.nrt.decref(builder, val_type, old_value)
            # write new
            setattr(dataval, attr, casted)

        sig = types.void(inst_type, types.literal(attr), val_type)
        return sig, codegen
Ejemplo n.º 4
0
 def _cmp_helper(self,op_str,other,negate):
     check_legal_cmp(self, op_str, other)
     opt_str = types.literal(types.unliteral(op_str))
     if(not isinstance(other,(VarTypeTemplate,Var))):
         return var_cmp_alpha(self,op_str,other, negate)
     else:
         return var_cmp_beta(self,op_str,other, negate)
Ejemplo n.º 5
0
def var_cmp_alpha(left_var, op_str, right_var,negated):
    from cre.condition_node import pt_to_cond, gen_pterm_ctor_alpha, gen_pterm_ctor_beta
    right_var_type = types.unliteral(types.literal(right_var))
    ctor = gen_pterm_ctor_alpha(left_var._numba_type_, op_str, right_var_type)
    pt = ctor(left_var, op_str, right_var)
    lbv = cast_structref(GenericVarType,left_var)
    return pt_to_cond(pt, lbv, None, negated)
Ejemplo n.º 6
0
    def test_literal_nested(self):
        @njit
        def foo(x):
            return literally(x) * 2

        @njit
        def bar(y, x):
            return foo(y) + x

        y, x = 3, 7
        self.assertEqual(bar(y, x), y * 2 + x)
        [foo_sig] = foo.signatures
        self.assertEqual(foo_sig[0], types.literal(y))
        [bar_sig] = bar.signatures
        self.assertEqual(bar_sig[0], types.literal(y))
        self.assertNotIsInstance(bar_sig[1], types.Literal)
def get_alpha_predicate_node(typ, attr_chain, op_str, literal_val):
    '''Gets a new instance of an AlphaPredicateNode that evals op(typ.attr, literal_val) '''
    right_type = types.literal(literal_val).literal_type

    dfn = get_alpha_predicate_node_definition(typ, attr_chain, op_str, right_type)
    ctor, left_attr_offsets = itemgetter('ctor', 'left_attr_offsets')(dfn)

    out = ctor(typ._fact_name, left_attr_offsets, literal_val)

    return out
Ejemplo n.º 8
0
    def test_literally_freevar(self):
        # Try referring to numba.literally not in the globals
        import numba

        @njit
        def foo(x):
            return numba.literally(x)

        self.assertEqual(foo(123), 123)
        self.assertEqual(foo.signatures[0][0], types.literal(123))
 def run_pass(self, state):
     repl = {}
     # Force the static_getitem to have a literal type as
     # index to replicate the problem.
     for inst, sig in state.calltypes.items():
         if (isinstance(inst, ir.Expr)
                 and inst.op == 'static_getitem'):
             [obj, idx] = sig.args
             new_sig = sig.replace(args=(obj,
                                         types.literal(inst.index)))
             repl[inst] = new_sig
     state.calltypes.update(repl)
     return True
Ejemplo n.º 10
0
    def test_literal_nested_multi_arg(self):
        @njit
        def foo(a, b, c):
            return inner(a, c)

        @njit
        def inner(x, y):
            return x + literally(y)

        kwargs = dict(a=1, b=2, c=3)
        got = foo(**kwargs)
        expect = (lambda a, b, c: a + c)(**kwargs)
        self.assertEqual(got, expect)
        [foo_sig] = foo.signatures
        self.assertEqual(foo_sig[2], types.literal(3))
Ejemplo n.º 11
0
def lower_getattr(typingctx, inst_type, attr_type):
    if (isinstance(attr_type, types.Literal)
            and isinstance(inst_type, types.StructRef)):

        attr = attr_type.literal_value
        fieldtype = inst_type.field_dict[attr]

        def codegen(context, builder, sig, args):
            [instance, attr_v] = args

            utils = _Utils(context, builder, inst_type)
            dataval = utils.get_data_struct(instance)
            ret = getattr(dataval, attr)
            return imputils.impl_ret_borrowed(context, builder, fieldtype, ret)

        sig = fieldtype(inst_type, types.literal(attr))
        return sig, codegen
Ejemplo n.º 12
0
 def resolve_dropna(self, ary, args, kws):
     out = ary
     # output is None for inplace case
     if 'inplace' in kws and kws['inplace'] == types.literal(True):
         out = types.none
     return signature(out, *args)

#### Struct Definitions ####

base_predicate_node_field_dict = {
    #### Attributes filled in at definition time ###
    "id_str" : unicode_type,
    "is_alpha" : u1,
    
    "left_fact_type_name" : unicode_type,
    "right_fact_type_name" : unicode_type,

    
    "left_attr_offsets" : i8[::1],#types.Any,
    # "filter_func" : filter_func_type,
    "op_str" : types.literal('=='),
    
    
    

    # #### Attributes filled in at link time ###
    
    # "left_t_id" : u8,
    # "left_facts" : VectorType, #Vector<*Fact>
    # "truth_values" : u1[:,:],
    # "kb_grow_queue" : VectorType,
    # "kb_change_queue" : VectorType,
    
}

from pprint import pprint