コード例 #1
0
    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')
コード例 #2
0
def build_type_map(mapper: Mapper, modules: List[MypyFile], graph: Graph,
                   types: Dict[Expression, Type], options: CompilerOptions,
                   errors: Errors) -> None:
    # Collect all classes defined in everything we are compiling
    classes = []
    for module in modules:
        module_classes = [
            node for node in module.defs if isinstance(node, ClassDef)
        ]
        classes.extend([(module, cdef) for cdef in module_classes])

    # Collect all class mappings so that we can bind arbitrary class name
    # references even if there are import cycles.
    for module, cdef in classes:
        class_ir = ClassIR(cdef.name,
                           module.fullname,
                           is_trait(cdef),
                           is_abstract=cdef.info.is_abstract)
        class_ir.is_ext_class = is_extension_class(cdef)
        # If global optimizations are disabled, turn of tracking of class children
        if not options.global_opts:
            class_ir.children = None
        mapper.type_to_ir[cdef.info] = class_ir

    # Populate structural information in class IR for extension classes.
    for module, cdef in classes:
        with catch_errors(module.path, cdef.line):
            if mapper.type_to_ir[cdef.info].is_ext_class:
                prepare_class_def(module.path, module.fullname, cdef, errors,
                                  mapper)
            else:
                prepare_non_ext_class_def(module.path, module.fullname, cdef,
                                          errors, mapper)

    # Collect all the functions also. We collect from the symbol table
    # so that we can easily pick out the right copy of a function that
    # is conditionally defined.
    for module in modules:
        for func in get_module_func_defs(module):
            prepare_func_def(module.fullname, None, func, mapper)
コード例 #3
0
 def setUp(self) -> None:
     self.inst_a = RInstance(ClassIR('A', '__main__'))
     self.inst_b = RInstance(ClassIR('B', '__main__'))
コード例 #4
0
 def prepare_class_def(self, cdef: ClassDef) -> None:
     ir = ClassIR(cdef.name,
                  [])  # Populate attributes later in visit_class_def
     self.classes.append(ir)
     self.mapper.type_to_ir[cdef.info] = ir
コード例 #5
0
 def test_set_attr(self) -> None:
     ir = ClassIR('A', [('x', BoolRType()), ('y', IntRType())])
     rtype = UserRType(ir)
     self.assert_emit(
         SetAttr(self.n, 'y', self.m, rtype),
         """CPY_SET_ATTR(cpy_r_n, 3, cpy_r_m, AObject, CPyTagged);""")
コード例 #6
0
 def test_get_attr(self) -> None:
     ir = ClassIR('A', [('x', BoolRType()), ('y', IntRType())])
     rtype = UserRType(ir)
     self.assert_emit(
         GetAttr(self.n, self.m, 'y', rtype),
         """cpy_r_n = CPY_GET_ATTR(cpy_r_m, 2, AObject, CPyTagged);""")