def generate_function_declaration(fn: FuncIR, emitter: Emitter) -> None: emitter.context.declarations[emitter.native_function_name(fn.decl)] = HeaderDeclaration( '{};'.format(native_function_header(fn.decl, emitter)), needs_export=True) if fn.name != TOP_LEVEL_NAME: emitter.context.declarations[PREFIX + fn.cname(emitter.names)] = HeaderDeclaration( '{};'.format(wrapper_function_header(fn, emitter.names)))
def generate_function_declaration(fn: FuncIR, emitter: Emitter) -> None: emitter.context.declarations[emitter.native_function_name(fn.decl)] = HeaderDeclaration( '{};'.format(native_function_header(fn.decl, emitter)), needs_export=True) if fn.name != TOP_LEVEL_NAME: if is_fastcall_supported(fn, emitter.capi_version): emitter.context.declarations[PREFIX + fn.cname(emitter.names)] = HeaderDeclaration( '{};'.format(wrapper_function_header(fn, emitter.names))) else: emitter.context.declarations[PREFIX + fn.cname(emitter.names)] = HeaderDeclaration( '{};'.format(legacy_wrapper_function_header(fn, emitter.names)))
def generate_object_struct(cl: ClassIR, emitter: Emitter) -> None: seen_attrs: Set[Tuple[str, RType]] = set() lines: List[str] = [] lines += ['typedef struct {', 'PyObject_HEAD', 'CPyVTableItem *vtable;'] if cl.has_method('__call__') and emitter.use_vectorcall(): lines.append('vectorcallfunc vectorcall;') 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 )
def declare_native_getters_and_setters(cl: ClassIR, emitter: Emitter) -> None: decls = emitter.context.declarations for attr, rtype in cl.attributes.items(): getter_name = native_getter_name(cl, attr, emitter.names) setter_name = native_setter_name(cl, attr, emitter.names) decls[getter_name] = HeaderDeclaration( '{}{}({} *self);'.format(emitter.ctype_spaced(rtype), getter_name, cl.struct_name(emitter.names)), needs_export=True, ) decls[setter_name] = HeaderDeclaration( 'bool {}({} *self, {}value);'.format( native_setter_name(cl, attr, emitter.names), cl.struct_name(emitter.names), emitter.ctype_spaced(rtype)), needs_export=True, )
def declare_finals( self, module: str, final_names: Iterable[Tuple[str, RType]], emitter: Emitter) -> None: for name, typ in final_names: static_name = emitter.static_name(name, module) emitter.context.declarations[static_name] = HeaderDeclaration( '{}{};'.format(emitter.ctype_spaced(typ), static_name), [self.final_definition(module, name, typ, emitter)], needs_export=True)
def generate_class_type_decl(cl: ClassIR, c_emitter: Emitter, external_emitter: Emitter, emitter: Emitter) -> None: context = c_emitter.context name = emitter.type_struct_name(cl) context.declarations[name] = HeaderDeclaration( 'PyTypeObject *{};'.format(emitter.type_struct_name(cl)), needs_export=True) # If this is a non-extension class, all we want is the type object decl. if not cl.is_ext_class: return generate_object_struct(cl, external_emitter) generate_full = not cl.is_trait and not cl.builtin_base if generate_full: context.declarations[emitter.native_function_name(cl.ctor)] = HeaderDeclaration( '{};'.format(native_function_header(cl.ctor, emitter)), needs_export=True, )
def declare_global(self, type_spaced: str, name: str, *, initializer: Optional[str] = None) -> None: if not initializer: defn = None else: defn = ['{}{} = {};'.format(type_spaced, name, initializer)] if name not in self.context.declarations: self.context.declarations[name] = HeaderDeclaration( '{}{};'.format(type_spaced, name), defn=defn, )
def declare_global(self, type_spaced: str, name: str, *, initializer: Optional[str] = None) -> None: if '[' not in type_spaced: base = '{}{}'.format(type_spaced, name) else: a, b = type_spaced.split('[', 1) base = '{}{}[{}'.format(a, name, b) if not initializer: defn = None else: defn = ['{} = {};'.format(base, initializer)] if name not in self.context.declarations: self.context.declarations[name] = HeaderDeclaration( '{};'.format(base), defn=defn, )
def declare_global(self, type_spaced: str, name: str, *, initializer: Optional[str] = None) -> None: if '[' not in type_spaced: base = f'{type_spaced}{name}' else: a, b = type_spaced.split('[', 1) base = f'{a}{name}[{b}' if not initializer: defn = None else: defn = [f'{base} = {initializer};'] if name not in self.context.declarations: self.context.declarations[name] = HeaderDeclaration( f'{base};', defn=defn, )