def setUp(self) -> None: self.env = Environment() self.n = self.env.add_local(Var('n'), int_rprimitive) self.m = self.env.add_local(Var('m'), int_rprimitive) self.k = self.env.add_local(Var('k'), int_rprimitive) self.l = self.env.add_local(Var('l'), list_rprimitive) # noqa self.ll = self.env.add_local(Var('ll'), list_rprimitive) self.o = self.env.add_local(Var('o'), object_rprimitive) self.o2 = self.env.add_local(Var('o2'), object_rprimitive) self.d = self.env.add_local(Var('d'), dict_rprimitive) self.b = self.env.add_local(Var('b'), bool_rprimitive) self.t = self.env.add_local(Var('t'), RTuple([int_rprimitive, bool_rprimitive])) self.tt = self.env.add_local( Var('tt'), RTuple( [RTuple([int_rprimitive, bool_rprimitive]), bool_rprimitive])) ir = ClassIR('A', 'mod') ir.attributes = OrderedDict([('x', bool_rprimitive), ('y', int_rprimitive)]) compute_vtable(ir) ir.mro = [ir] self.r = self.env.add_local(Var('r'), RInstance(ir)) self.context = EmitterContext(NameGenerator([['mod']])) self.emitter = Emitter(self.context, self.env) self.declarations = Emitter(self.context, self.env) self.visitor = FunctionEmitterVisitor(self.emitter, self.declarations, 'prog.py', 'prog')
def setUp(self) -> None: self.env = Environment() self.n = self.env.add_local(Var('n'), int_rprimitive) self.m = self.env.add_local(Var('m'), int_rprimitive) self.k = self.env.add_local(Var('k'), int_rprimitive) self.l = self.env.add_local(Var('l'), list_rprimitive) # noqa self.ll = self.env.add_local(Var('ll'), list_rprimitive) self.o = self.env.add_local(Var('o'), object_rprimitive) self.o2 = self.env.add_local(Var('o2'), object_rprimitive) self.d = self.env.add_local(Var('d'), dict_rprimitive) self.b = self.env.add_local(Var('b'), bool_rprimitive) self.s1 = self.env.add_local(Var('s1'), short_int_rprimitive) self.s2 = self.env.add_local(Var('s2'), short_int_rprimitive) self.i32 = self.env.add_local(Var('i32'), int32_rprimitive) self.i32_1 = self.env.add_local(Var('i32_1'), int32_rprimitive) self.i64 = self.env.add_local(Var('i64'), int64_rprimitive) self.i64_1 = self.env.add_local(Var('i64_1'), int64_rprimitive) self.ptr = self.env.add_local(Var('ptr'), pointer_rprimitive) self.t = self.env.add_local(Var('t'), RTuple([int_rprimitive, bool_rprimitive])) self.tt = self.env.add_local( Var('tt'), RTuple( [RTuple([int_rprimitive, bool_rprimitive]), bool_rprimitive])) ir = ClassIR('A', 'mod') ir.attributes = OrderedDict([('x', bool_rprimitive), ('y', int_rprimitive)]) compute_vtable(ir) ir.mro = [ir] self.r = self.env.add_local(Var('r'), RInstance(ir)) self.context = EmitterContext(NameGenerator([['mod']]))
def __init__(self, items: List[Value], line: int) -> None: super().__init__(line) self.items = items # Don't keep track of the fact that an int is short after it # is put into a tuple, since we don't properly implement # runtime subtyping for tuples. self.tuple_type = RTuple( [arg.type if not is_short_int_rprimitive(arg.type) else int_rprimitive for arg in items]) self.type = self.tuple_type
def test_names(self) -> None: assert RTuple([int_rprimitive, int_rprimitive]).unique_id == "T2II" assert RTuple([list_rprimitive, object_rprimitive, self.inst_a]).unique_id == "T3OOO" assert RTuple([list_rprimitive, object_rprimitive, self.inst_b]).unique_id == "T3OOO" assert RTuple([]).unique_id == "T0" assert RTuple([RTuple([]), RTuple([int_rprimitive, int_rprimitive])]).unique_id == "T2T0T2II" assert RTuple( [bool_rprimitive, RUnion([bool_rprimitive, int_rprimitive])]).unique_id == "T2CO"
def setUp(self) -> None: self.registers = [] # type: List[Register] def add_local(name: str, rtype: RType) -> Register: reg = Register(rtype, name) self.registers.append(reg) return reg self.n = add_local('n', int_rprimitive) self.m = add_local('m', int_rprimitive) self.k = add_local('k', int_rprimitive) self.l = add_local('l', list_rprimitive) # noqa self.ll = add_local('ll', list_rprimitive) self.o = add_local('o', object_rprimitive) self.o2 = add_local('o2', object_rprimitive) self.d = add_local('d', dict_rprimitive) self.b = add_local('b', bool_rprimitive) self.s1 = add_local('s1', short_int_rprimitive) self.s2 = add_local('s2', short_int_rprimitive) self.i32 = add_local('i32', int32_rprimitive) self.i32_1 = add_local('i32_1', int32_rprimitive) self.i64 = add_local('i64', int64_rprimitive) self.i64_1 = add_local('i64_1', int64_rprimitive) self.ptr = add_local('ptr', pointer_rprimitive) self.t = add_local('t', RTuple([int_rprimitive, bool_rprimitive])) self.tt = add_local( 'tt', RTuple( [RTuple([int_rprimitive, bool_rprimitive]), bool_rprimitive])) ir = ClassIR('A', 'mod') ir.attributes = OrderedDict([('x', bool_rprimitive), ('y', int_rprimitive)]) compute_vtable(ir) ir.mro = [ir] self.r = add_local('r', RInstance(ir)) self.context = EmitterContext(NameGenerator([['mod']]))
def type_to_rtype(self, typ: Optional[Type]) -> RType: if typ is None: return object_rprimitive typ = get_proper_type(typ) if isinstance(typ, Instance): if typ.type.fullname == 'builtins.int': return int_rprimitive elif typ.type.fullname == 'builtins.float': return float_rprimitive elif typ.type.fullname == 'builtins.str': return str_rprimitive elif typ.type.fullname == 'builtins.bool': return bool_rprimitive elif typ.type.fullname == 'builtins.list': return list_rprimitive # Dict subclasses are at least somewhat common and we # specifically support them, so make sure that dict operations # get optimized on them. elif any(cls.fullname == 'builtins.dict' for cls in typ.type.mro): return dict_rprimitive elif typ.type.fullname == 'builtins.set': return set_rprimitive elif typ.type.fullname == 'builtins.tuple': return tuple_rprimitive # Varying-length tuple elif typ.type.fullname == 'builtins.range': return range_rprimitive elif typ.type in self.type_to_ir: inst = RInstance(self.type_to_ir[typ.type]) # Treat protocols as Union[protocol, object], so that we can do fast # method calls in the cases where the protocol is explicitly inherited from # and fall back to generic operations when it isn't. if typ.type.is_protocol: return RUnion([inst, object_rprimitive]) else: return inst else: return object_rprimitive elif isinstance(typ, TupleType): # Use our unboxed tuples for raw tuples but fall back to # being boxed for NamedTuple. if typ.partial_fallback.type.fullname == 'builtins.tuple': return RTuple([self.type_to_rtype(t) for t in typ.items]) else: return tuple_rprimitive elif isinstance(typ, CallableType): return object_rprimitive elif isinstance(typ, NoneTyp): return none_rprimitive elif isinstance(typ, UnionType): return RUnion([self.type_to_rtype(item) for item in typ.items]) elif isinstance(typ, AnyType): return object_rprimitive elif isinstance(typ, TypeType): return object_rprimitive elif isinstance(typ, TypeVarType): # Erase type variable to upper bound. # TODO: Erase to union if object has value restriction? return self.type_to_rtype(typ.upper_bound) elif isinstance(typ, PartialType): assert typ.var.type is not None return self.type_to_rtype(typ.var.type) elif isinstance(typ, Overloaded): return object_rprimitive elif isinstance(typ, TypedDictType): return dict_rprimitive elif isinstance(typ, LiteralType): return self.type_to_rtype(typ.fallback) elif isinstance(typ, (UninhabitedType, UnboundType)): # Sure, whatever! return object_rprimitive # I think we've covered everything that is supposed to # actually show up, so anything else is a bug somewhere. assert False, 'unexpected type %s' % type(typ)
def get_dict_item_type(self, expr: Expression) -> RType: key_type = self.get_dict_key_type(expr) value_type = self.get_dict_value_type(expr) return RTuple([key_type, value_type])
# Like next_raw_op, don't swallow StopIteration, # but also don't propagate an error. # Can return NULL: see next_op. send_op = custom_op(name='send', arg_types=[object_rprimitive, object_rprimitive], result_type=object_rprimitive, error_kind=ERR_NEVER, emit=call_emit('CPyIter_Send')) # This is sort of unfortunate but oh well: yield_from_except performs most of the # error handling logic in `yield from` operations. It returns a bool and a value. # If the bool is true, then a StopIteration was received and we should return. # If the bool is false, then the value should be yielded. # The normal case is probably that it signals an exception, which gets # propagated. yield_from_rtuple = RTuple([bool_rprimitive, object_rprimitive]) # Op used for "yield from" error handling. # See comment in CPy_YieldFromErrorHandle for more information. yield_from_except_op = custom_op( name='yield_from_except', arg_types=[object_rprimitive], result_type=yield_from_rtuple, error_kind=ERR_MAGIC, emit=simple_emit( '{dest}.f0 = CPy_YieldFromErrorHandle({args[0]}, &{dest}.f1);')) # Create method object from a callable object and self. method_new_op = custom_op(name='method_new', arg_types=[object_rprimitive, object_rprimitive], result_type=object_rprimitive,