Exemplo n.º 1
0
def impl_numba_typeref_ctor(cls):
    """
    Defines ``Dict()``, the type-inferred version of the dictionary ctor.

    Parameters
    ----------
    cls : TypeRef
        Expecting a TypeRef of a precise DictType.

    See also: `redirect_type_ctor` in numba/target/bulitins.py
    """
    dict_ty = cls.instance_type
    if not isinstance(dict_ty, types.DictType):
        msg = "expecting a DictType but got {}".format(dict_ty)
        return  # reject
    # Ensure the dictionary is precisely typed.
    if not dict_ty.is_precise():
        msg = "expecting a precise DictType but got {}".format(dict_ty)
        raise errors.LoweringError(msg)

    key_type = types.TypeRef(dict_ty.key_type)
    value_type = types.TypeRef(dict_ty.value_type)

    def impl(cls):
        # Simply call .empty() with the key/value types from *cls*
        return Dict.empty(key_type, value_type)

    return impl
Exemplo n.º 2
0
    def __getattr__(self, attr):
        # print("DEREF", attr)
        if(attr == 'fact_type'):
            fact_type = self._numba_type_.field_dict['fact_type']
            return fact_type.instance_type if(fact_type != types.Any) else None
        elif(attr == 'head_type'):
            head_type = self._numba_type_.field_dict['head_type']
            return head_type.instance_type if(head_type != types.Any) else None
        elif(attr == 'deref_attrs'):
            return var_get_deref_attrs(self)
        elif(attr == 'alias'):
            return var_get_alias(self)
        elif(attr == 'fact_type_name'):
            return var_get_fact_type_name(self)
        elif(True): 
            typ = self._numba_type_
            
            fact_type = typ.field_dict['fact_type'].instance_type 
            fact_type_name = fact_type._fact_name
            head_type = fact_type.field_dict[attr]

            fd = fact_type.field_dict
            offset = fact_type._attr_offsets[list(fd.keys()).index(attr)]
            struct_type = get_var_definition(types.TypeRef(fact_type), types.TypeRef(head_type))
            new = var_ctor(struct_type, fact_type_name, var_get_alias(self))
            copy_and_append(self, new,attr, offset)
            return new
Exemplo n.º 3
0
def typeof_typeref(val, c):
    if isinstance(val, types.BaseFunction):
        return val
    elif isinstance(val, (types.Number, types.Boolean)):
        return types.NumberClass(val)
    else:
        return types.TypeRef(val)
Exemplo n.º 4
0
def impl_numba_typeref_ctor(cls):
    """
    Defines ``List()``, the type-inferred version of the list ctor.

    Parameters
    ----------
    cls : TypeRef
        Expecting a TypeRef of a precise ListType.

    See also: `redirect_type_ctor` in numba/target/bulitins.py
    """
    list_ty = cls.instance_type
    if not isinstance(list_ty, types.ListType):
        msg = "expecting a ListType but got {}".format(list_ty)
        return  # reject
    # Ensure the list is precisely typed.
    if not list_ty.is_precise():
        msg = "expecting a precise ListType but got {}".format(list_ty)
        raise errors.LoweringError(msg)

    item_type = types.TypeRef(list_ty.item_type)

    def impl(cls):
        # Simply call .empty_list with the item types from *cls*
        return List.empty_list(item_type)

    return impl
Exemplo n.º 5
0
def typeof_type(val, c):
    """
    Type various specific Python types.
    """
    if issubclass(val, BaseException):
        return types.ExceptionClass(val)
    if issubclass(val, tuple) and hasattr(val, "_asdict"):
        return types.NamedTupleClass(val)

    if issubclass(val, np.generic):
        return types.NumberClass(numpy_support.from_dtype(val))

    from numba.typed import Dict
    if issubclass(val, Dict):
        return types.TypeRef(types.DictType)

    from numba.typed import List
    if issubclass(val, List):
        return types.TypeRef(types.ListType)
Exemplo n.º 6
0
    def generic_resolve(self, typ, attr):
        if attr in typ.field_dict:
            attrty = typ.field_dict[attr]
            return attrty
        head_type = typ.field_dict['head_type'].instance_type 
        #TODO Should check that all subtype references are valid
        if(not hasattr(head_type,'field_dict')):
            raise AttributeError(f"Cannot dereference attribute '{attr}' of {typ}.")

        fact_type = typ.field_dict['fact_type']
        if(attr in head_type.field_dict):
            new_head_type = types.TypeRef(head_type.field_dict[attr])
            field_dict = {
                **var_fields_dict,
                **{"fact_type" : fact_type,
                 "head_type" : new_head_type}
            }
            attrty = VarTypeTemplate([(k,v) for k,v, in field_dict.items()])
            return attrty
        else:
            raise AttributeError(f"Var[{fact_type}] has no attribute '{attr}'")
}

from pprint import pprint

basepredicate_node_fields = [(k,v) for k,v, in base_predicate_node_field_dict.items()]
# pprint(basepredicate_node_fields)
BasePredicateNode, BasePredicateNodeType = define_structref("BasePredicateNode", base_subscriber_fields + basepredicate_node_fields)


alpha_predicate_node_field_dict = {
    **base_predicate_node_field_dict,
    # "truth_values" : u1[:],

    #### Attributes filled in at definition time ###
    "filter_func" : alpha_filter_func_type,
    "left_type" : types.TypeRef(BaseFactType), #<- Filled in at definition
    "right_val" : types.float64, #<- Can be specialized to something else
    
}
alpha_predicate_node_fields = [(k,v) for k,v, in alpha_predicate_node_field_dict.items()]
# pprint(alpha_predicate_node_fields)
AlphaPredicateNode, AlphaPredicateNodeTemplate = define_structref_template("AlphaPredicateNode",
             base_subscriber_fields + alpha_predicate_node_fields, define_constructor=False)

GenericAlphaPredicateNodeType = AlphaPredicateNodeTemplate(base_subscriber_fields + alpha_predicate_node_fields)
# print(GenericAlphaPredicateNodeType)

beta_predicate_node_field_dict = {
    **base_predicate_node_field_dict,
    
Exemplo n.º 8
0
 def __new__(cls, typ, alias=None):
     fact_type_name = typ._fact_name
     typ = types.TypeRef(typ)
     struct_type = get_var_definition(typ,typ)
     st = var_ctor(struct_type, fact_type_name, alias)
     return st