def generate_native_function(fn: FuncIR, emitter: Emitter, source_path: str, module_name: str) -> None: declarations = Emitter(emitter.context, fn.env) body = Emitter(emitter.context, fn.env) visitor = FunctionEmitterVisitor(body, declarations, fn.name, source_path, module_name) declarations.emit_line('{} {{'.format(native_function_header(fn, emitter))) body.indent() for r, i in fn.env.indexes.items(): if isinstance(r.type, RTuple): emitter.declare_tuple_struct(r.type) if i < len(fn.args): continue # skip the arguments ctype = emitter.ctype_spaced(r.type) declarations.emit_line('{ctype}{prefix}{name};'.format( ctype=ctype, prefix=REG_PREFIX, name=r.name)) # Before we emit the blocks, give them all labels for i, block in enumerate(fn.blocks): block.label = i for block in fn.blocks: body.emit_label(block) for op in block.ops: op.accept(visitor) body.emit_line('}') emitter.emit_from_emitter(declarations) emitter.emit_from_emitter(body)
def generate_object_struct(cl: ClassIR, emitter: Emitter) -> None: seen_attrs = set() # type: Set[Tuple[str, RType]] lines = [] # type: List[str] lines += ['typedef struct {', 'PyObject_HEAD', 'CPyVTableItem *vtable;'] for base in reversed(cl.base_mro): if not base.is_trait: for attr, rtype in base.attributes.items(): if (attr, rtype) not in seen_attrs: lines.append('{}{};'.format(emitter.ctype_spaced(rtype), emitter.attr(attr))) seen_attrs.add((attr, rtype)) if isinstance(rtype, RTuple): emitter.declare_tuple_struct(rtype) lines.append('}} {};'.format(cl.struct_name(emitter.names))) lines.append('') emitter.context.declarations[cl.struct_name( emitter.names)] = HeaderDeclaration(lines, is_type=True)