def generate_slot_map_op(ops: List[str], op: str, base: TypeInfo, typ: TypeInfo) -> None: ops.append('def {}(t)'.format(op)) nslots = num_slots(typ) slots = compile_slot_mapping(base) a = [] # type: List[str] for t in slots: a.append(transform_type_to_runtime_repr(t)) for i in range(len(slots), nslots): a.append('__Dyn') ops.append(' return [' + ', '.join(a) + ']') ops.append('end')
This is added to a generic wrapper class. """ # The type is 'any' since it should behave covariantly in subclasses. return [VarDef([(Var(self.object_member_name(tdef.info)), Any())], False, None)] str object_member_name(self, TypeInfo info): if self.tf.is_java: return '__o_{}'.format(info.name) else: return '__o' FuncDef make_generic_wrapper_init(self, TypeInfo info): """Build constructor of a generic wrapper class.""" nslots = num_slots(info) cdefs = <Node> [] # Build superclass constructor call. if info.base.full_name() != 'builtins.object' and self.tf.is_java: s = SuperExpr('__init__') cargs = <Node> [NameExpr('__o')] for n in range(num_slots(info.base)): cargs.append(NameExpr(tvar_arg_name(n + 1))) for n in range(num_slots(info.base)): cargs.append(NameExpr(tvar_arg_name(n + 1, BOUND_VAR))) c = CallExpr(s, cargs, [nodes.ARG_POS] * len(cargs)) cdefs.append(ExpressionStmt(c)) # Create initialization of the wrapped object.
void add_slot_map_support_for_type_pair(str[] map, str[] ops, TypeInfo base, TypeInfo typ): op = '__{}TypeTo{}Slots'.format(base.name(), typ.name()) map.append(' ({}, {}) : {},'.format(base.name(), typ.name(), op)) if typ.is_generic(): map.append(' ({}, {}) : {},'.format(base.name(), typ.name() + dynamic_suffix(False), op)) generate_slot_map_op(ops, op, base, typ) void generate_slot_map_op(str[] ops, str op, TypeInfo base, TypeInfo typ): ops.append('def {}(t)'.format(op)) nslots = num_slots(typ) slots = compile_slot_mapping(base) a = <str> [] for t in slots: a.append(transform_type_to_runtime_repr(t)) for i in range(len(slots), nslots): a.append('__Dyn') ops.append(' return [' + ', '.join(a) + ']') ops.append('end') str transform_type_to_runtime_repr(Type t): if isinstance(t, Instance): inst = (Instance)t if inst.args == []: return inst.type.name()
Type[] compile_slot_mapping(TypeInfo typ): """Return types that represent values of type variable slots of a type. The returned types are in terms of type variables of the type. For example, assume these definitions: . class C<T, S>(D<E<S>>): ... . class D<S>(object): ... Now slot mappings for C is [E<S>, T] (S and T refer to type variables of C). """ Type[] exprs = [] for slot in range(num_slots(typ)): # Figure out the superclass which defines the slot; also figure out # the tvar index that maps to the slot. origin, tv = find_slot_origin(typ, slot) # Map self type to the superclass -> extract tvar with target index # (only contains subclass tvars?? PROBABLY NOT). selftype = self_type(typ) selftype = map_instance_to_supertype(selftype, origin) tvar = selftype.args[tv - 1] # tvar is the representation of the slot in terms of type arguments. exprs.append(tvar) return exprs