def get_default() -> Value: assert arg.initializer is not None # If it is constant, don't bother storing it if is_constant(arg.initializer): return builder.accept(arg.initializer) # Because gen_arg_defaults runs before calculate_arg_defaults, we # add the static/attribute to final_names/the class here. elif not builder.fn_info.is_nested: name = fitem.fullname + '.' + arg.variable.name builder.final_names.append((name, target.type)) return builder.add(LoadStatic(target.type, name, builder.module_name)) else: name = arg.variable.name builder.fn_info.callable_class.ir.attributes[name] = target.type return builder.add( GetAttr(builder.fn_info.callable_class.self_reg, name, arg.line))
def allocate_class(builder: IRBuilder, cdef: ClassDef) -> Value: # OK AND NOW THE FUN PART base_exprs = cdef.base_type_exprs + cdef.removed_base_type_exprs if base_exprs: bases = [builder.accept(x) for x in base_exprs] tp_bases = builder.new_tuple(bases, cdef.line) else: tp_bases = builder.add( LoadErrorValue(object_rprimitive, is_borrowed=True)) modname = builder.load_static_unicode(builder.module_name) template = builder.add( LoadStatic(object_rprimitive, cdef.name + "_template", builder.module_name, NAMESPACE_TYPE)) # Create the class tp = builder.call_c(pytype_from_template_op, [template, tp_bases, modname], cdef.line) # Immediately fix up the trait vtables, before doing anything with the class. ir = builder.mapper.type_to_ir[cdef.info] if not ir.is_trait and not ir.builtin_base: builder.add( Call( FuncDecl(cdef.name + '_trait_vtable_setup', None, builder.module_name, FuncSignature([], bool_rprimitive)), [], -1)) # Populate a '__mypyc_attrs__' field containing the list of attrs builder.call_c(py_setattr_op, [ tp, builder.load_static_unicode('__mypyc_attrs__'), create_mypyc_attrs_tuple(builder, builder.mapper.type_to_ir[cdef.info], cdef.line) ], cdef.line) # Save the class builder.add(InitStatic(tp, cdef.name, builder.module_name, NAMESPACE_TYPE)) # Add it to the dict builder.call_c(dict_set_item_op, [ builder.load_globals_dict(), builder.load_static_unicode(cdef.name), tp, ], cdef.line) return tp
def load_static_checked(self, typ: RType, identifier: str, module_name: Optional[str] = None, namespace: str = NAMESPACE_STATIC, line: int = -1, error_msg: Optional[str] = None) -> Value: if error_msg is None: error_msg = "name '{}' is not defined".format(identifier) ok_block, error_block = BasicBlock(), BasicBlock() value = self.add( LoadStatic(typ, identifier, module_name, namespace, line=line)) self.add( Branch(value, error_block, ok_block, Branch.IS_ERROR, rare=True)) self.activate_block(error_block) self.add( RaiseStandardError(RaiseStandardError.NAME_ERROR, error_msg, line)) self.add(Unreachable()) self.activate_block(ok_block) return value
def load_final_static(self, fullname: str, typ: RType, line: int, error_name: Optional[str] = None) -> Value: if error_name is None: error_name = fullname ok_block, error_block = BasicBlock(), BasicBlock() split_name = split_target(self.graph, fullname) assert split_name is not None value = self.add( LoadStatic(typ, split_name[1], split_name[0], line=line)) self.add( Branch(value, error_block, ok_block, Branch.IS_ERROR, rare=True)) self.activate_block(error_block) self.add( RaiseStandardError( RaiseStandardError.VALUE_ERROR, 'value for final name "{}" was not set'.format(error_name), line)) self.add(Unreachable()) self.activate_block(ok_block) return value
def load_native_type_object(self, fullname: str) -> Value: module, name = fullname.rsplit('.', 1) return self.add(LoadStatic(object_rprimitive, name, module, NAMESPACE_TYPE))
def load_module(self, name: str) -> Value: return self.add(LoadStatic(object_rprimitive, name, namespace=NAMESPACE_MODULE))
def load_globals_dict(self) -> Value: return self.add( LoadStatic(dict_rprimitive, 'globals', self.module_name))
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))
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))
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))