Exemplo n.º 1
0
 def load_static_int(self, value: int) -> Value:
     """Loads a static integer Python 'int' object into a register."""
     if abs(value) > MAX_LITERAL_SHORT_INT:
         static_symbol = self.literal_static_name(value)
         return self.add(LoadStatic(int_rprimitive, static_symbol, ann=value))
     else:
         return self.add(LoadInt(value))
Exemplo n.º 2
0
    def load_static_unicode(self, value: str) -> Value:
        """Loads a static unicode value into a register.

        This is useful for more than just unicode literals; for example, method calls
        also require a PyObject * form for the name of the method.
        """
        static_symbol = self.literal_static_name(value)
        return self.add(LoadStatic(str_rprimitive, static_symbol, ann=value))
Exemplo n.º 3
0
    def load_static_module_attr(self, expr: RefExpr) -> Register:
        target = self.alloc_target(self.node_type(expr))
        module = '.'.join(expr.node.fullname().split('.')[:-1])
        right = expr.node.fullname().split('.')[-1]
        left = self.alloc_temp(ObjectRType())
        self.add(LoadStatic(left, c_module_name(module)))
        self.add(PyGetAttr(target, left, right))

        return target
Exemplo n.º 4
0
    def allocate_class(self, cdef: ClassDef) -> Value:
        # OK AND NOW THE FUN PART
        base_exprs = cdef.base_type_exprs + cdef.removed_base_type_exprs
        if base_exprs:
            bases = [self.accept(x) for x in base_exprs]
            tp_bases = self.primitive_op(new_tuple_op, bases, cdef.line)
        else:
            tp_bases = self.add(
                LoadErrorValue(object_rprimitive, is_borrowed=True))
        modname = self.load_static_unicode(self.module_name)
        template = self.add(
            LoadStatic(object_rprimitive, cdef.name + "_template",
                       self.module_name, NAMESPACE_TYPE))
        # Create the class
        tp = self.primitive_op(pytype_from_template_op,
                               [template, tp_bases, modname], cdef.line)
        # Immediately fix up the trait vtables, before doing anything with the class.
        ir = self.mapper.type_to_ir[cdef.info]
        if not ir.is_trait and not ir.builtin_base:
            self.add(
                Call(
                    FuncDecl(cdef.name + '_trait_vtable_setup', None,
                             self.module_name,
                             FuncSignature([], bool_rprimitive)), [], -1))
        # Populate a '__mypyc_attrs__' field containing the list of attrs
        self.primitive_op(py_setattr_op, [
            tp,
            self.load_static_unicode('__mypyc_attrs__'),
            self.create_mypyc_attrs_tuple(self.mapper.type_to_ir[cdef.info],
                                          cdef.line)
        ], cdef.line)

        # Save the class
        self.add(InitStatic(tp, cdef.name, self.module_name, NAMESPACE_TYPE))

        # Add it to the dict
        self.primitive_op(dict_set_item_op, [
            self.builder.load_globals_dict(),
            self.load_static_unicode(cdef.name),
            tp,
        ], cdef.line)

        return tp
Exemplo n.º 5
0
 def load_native_type_object(self, fullname: str) -> Value:
     module, name = fullname.rsplit('.', 1)
     return self.add(
         LoadStatic(object_rprimitive, name, module, NAMESPACE_TYPE))
Exemplo n.º 6
0
 def load_module(self, name: str) -> Value:
     return self.add(
         LoadStatic(object_rprimitive, name, namespace=NAMESPACE_MODULE))
Exemplo n.º 7
0
 def load_static_complex(self, value: complex) -> Value:
     """Loads a static complex value into a register."""
     static_symbol = self.literal_static_name(value)
     return self.add(LoadStatic(object_rprimitive, static_symbol,
                                ann=value))
Exemplo n.º 8
0
 def load_static_bytes(self, value: bytes) -> Value:
     """Loads a static bytes value into a register."""
     static_symbol = self.literal_static_name(value)
     return self.add(LoadStatic(object_rprimitive, static_symbol,
                                ann=value))
Exemplo n.º 9
0
 def load_static_float(self, value: float) -> Value:
     """Loads a static float value into a register."""
     static_symbol = self.literal_static_name(value)
     return self.add(LoadStatic(float_rprimitive, static_symbol, ann=value))
Exemplo n.º 10
0
 def load_static_int(self, value: int) -> Value:
     """Loads a static integer Python 'int' object into a register."""
     static_symbol = self.literal_static_name(value)
     return self.add(LoadStatic(int_rprimitive, static_symbol, ann=value))