Example #1
0
def generate_type_map_op(ops: List[str], op: str, typ, alt) -> None:
    ops.append('def {}(v)'.format(op))
    a = []  # type: List[str]
    for i in range(len(typ.type_vars)):
        p = get_tvar_access_path(typ, i + 1)
        expr = 'v.' + tvar_slot_name(p[0] - 1, alt)
        for j in p[1:]:
            expr += '.args[{}]'.format(j - 1)
        a.append(expr)
    ops.append('  return [{}]'.format(', '.join(a)))
    ops.append('end')
Example #2
0
def generate_type_map_op(ops: List[str], op: str, typ, alt) -> None:
    ops.append('def {}(v)'.format(op))
    a = [] # type: List[str]
    for i in range(len(typ.type_vars)):
        p = get_tvar_access_path(typ, i + 1)
        expr = 'v.' + tvar_slot_name(p[0] - 1, alt)
        for j in p[1:]:
            expr += '.args[{}]'.format(j - 1)
        a.append(expr)
    ops.append('  return [{}]'.format(', '.join(a)))
    ops.append('end')
Example #3
0
        
        self.make_wrapper_slot_initializer(fdef)
        
        return fdef
    
    Node[] make_tvar_representation(self, TypeInfo info, any is_alt=False):
        """Return type variable slot member definitions.

        There are of form 'any __tv*'. Only include new slots defined in the
        type.
        """
        Node[] defs = []
        base_slots = num_slots(info.base)
        for n in range(len(info.type_vars)):
            # Only include a type variable if it introduces a new slot.
            slot = get_tvar_access_path(info, n + 1)[0] - 1
            if slot >= base_slots:
                defs.append(VarDef([(Var(tvar_slot_name(slot, is_alt)),
                                     Any())], False, None))
        return defs
    
    void make_instance_tvar_initializer(self, FuncDef creat):
        """Add type variable member initialization code to a constructor.

        Modify the constructor body directly.
        """
        for n in range(num_slots(creat.info)):
            rvalue = self.make_tvar_init_expression(creat.info, n)
            init = AssignmentStmt([MemberExpr(self_expr(),
                                              tvar_slot_name(n),
                                              direct=True)],
Example #4
0
                td = (TypeDef)d
                if td.info is not None:
                    add_type_map_support_for_type(map, ops, td.info, alt,
                                                  suffix)
        map.append('  )')
    map.append('end')
    return '\n'.join(map) + '\n' + '\n'.join(ops)


void add_type_map_support_for_type(str[] map, str[] ops, TypeInfo typ, alt,
                                   str suffix):
    op = '__{}ValueToType{}'.format(typ.name(), suffix)
    map.append('    {} : {},'.format(typ.name(), op))
    if typ.is_generic():
        map.append('    {} : {},'.format(typ.name() + dynamic_suffix(False),
                                         op))
    generate_type_map_op(ops, op, typ, alt)


void generate_type_map_op(str[] ops, str op, typ, alt):
    ops.append('def {}(v)'.format(op))
    a = <str> []
    for i in range(len(typ.type_vars)):
        p = get_tvar_access_path(typ, i + 1)
        expr = 'v.' + tvar_slot_name(p[0] - 1, alt)
        for j in p[1:]:
            expr += '.args[{}]'.format(j - 1)
        a.append(expr)
    ops.append('  return [{}]'.format(', '.join(a)))
    ops.append('end')
Example #5
0
        exprs.append(tvar)
    
    return exprs


tuple<TypeInfo, int> find_slot_origin(TypeInfo info, int slot):
    """Determine class and type variable index that directly maps to the slot.

    The result defines which class in inheritance hierarchy of info introduced
    the slot. All subclasses inherit this slot. The result TypeInfo always
    refers to one of the base classes of info (or info itself).

    Examples:
      - In 'class C<T>: ...', the slot 0 in C is mapped to type var 1 (T) in C.
      - In 'class D<S, U>(C<U>): ...', the slot 0 in D is mapped to type var
        1 (T) in C; the slot 1 of D is mapped to type variable 1 of D.
    """
    base = info.base
    super_slots = num_slots(base)
    if slot < super_slots:
        # A superclass introduced the slot.
        return find_slot_origin(base, slot)
    else:
        # This class introduced the slot. Figure out which type variable maps
        # to the slot.
        for tv in range(1, len(info.type_vars) + 1):
            if get_tvar_access_path(info, tv)[0] - 1 == slot:
                return (info, tv)
        
        raise RuntimeError('Could not map slot')