Example #1
0
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)))
Example #2
0
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)))

    generate_object_struct(cl, external_emitter)
    declare_native_getters_and_setters(cl, 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)))
Example #3
0
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,
        )
Example #4
0
 def declare_global(self, type_spaced: str, name: str, static: bool=True) -> None:
     static_str = 'static ' if static else ''
     if name not in self.context.declarations:
         self.context.declarations[name] = HeaderDeclaration(
             set(),
             ['{}{}{};'.format(static_str, type_spaced, name)],
         )
Example #5
0
 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),
             needs_export=True)
Example #6
0
 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,
         )
Example #7
0
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:
        declare_native_getters_and_setters(cl, emitter)

        context.declarations[emitter.native_function_name(cl.ctor)] = HeaderDeclaration(
            '{};'.format(native_function_header(cl.ctor, emitter)),
            needs_export=True,
        )
Example #8
0
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))
    lines.append('}} {};'.format(cl.struct_name(emitter.names)))
    lines.append('')
    emitter.context.declarations[cl.struct_name(
        emitter.names)] = HeaderDeclaration(lines, is_type=True)
Example #9
0
 def declare_global(self,
                    type_spaced: str,
                    name: str,
                    static: bool = True,
                    initializer: Optional[str] = None) -> None:
     initializer_body = '' if not initializer else ' = {}'.format(
         initializer)
     static_str = 'static ' if static else ''
     if name not in self.context.declarations:
         self.context.declarations[name] = HeaderDeclaration(
             set(),
             [
                 '{}{}{}{};'.format(static_str, type_spaced, name,
                                    initializer_body)
             ],
         )