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 read(self, target: Union[Value, AssignmentTarget], line: int = -1) -> Value: if isinstance(target, Value): return target if isinstance(target, AssignmentTargetRegister): return target.register if isinstance(target, AssignmentTargetIndex): reg = self.gen_method_call(target.base, '__getitem__', [target.index], target.type, line) if reg is not None: return reg assert False, target.base.type if isinstance(target, AssignmentTargetAttr): if isinstance(target.obj.type, RInstance) and target.obj.type.class_ir.is_ext_class: return self.add(GetAttr(target.obj, target.attr, line)) else: return self.py_get_attr(target.obj, target.attr, line) assert False, 'Unsupported lvalue: %r' % target
def load_outer_env(builder: IRBuilder, base: Value, outer_env: Dict[SymbolNode, SymbolTarget]) -> Value: """Load the environment class for a given base into a register. Additionally, iterates through all of the SymbolNode and AssignmentTarget instances of the environment at the given index's symtable, and adds those instances to the environment of the current environment. This is done so that the current environment can access outer environment variables without having to reload all of the environment registers. Returns the register where the environment class was loaded. """ env = builder.add(GetAttr(base, ENV_ATTR_NAME, builder.fn_info.fitem.line)) assert isinstance(env.type, RInstance), f'{env} must be of type RInstance' for symbol, target in outer_env.items(): env.type.class_ir.attributes[symbol.name] = target.type symbol_target = AssignmentTargetAttr(env, symbol.name) builder.add_target(symbol, symbol_target) return env
def test_get_attr(self) -> None: self.assert_emit( GetAttr(self.r, 'y', 1), """cpy_r_r0 = native_A_gety((mod___AObject *)cpy_r_r); /* y */""")