コード例 #1
0
def build_ir_for_single_file(input_lines: List[str],
                             compiler_options: Optional[CompilerOptions] = None) -> List[FuncIR]:
    program_text = '\n'.join(input_lines)

    compiler_options = compiler_options or CompilerOptions()
    options = Options()
    options.show_traceback = True
    options.use_builtins_fixtures = True
    options.strict_optional = True
    options.python_version = (3, 6)
    options.export_types = True
    options.preserve_asts = True
    options.per_module_options['__main__'] = {'mypyc': True}

    source = build.BuildSource('main', '__main__', program_text)
    # Construct input as a single single.
    # Parse and type check the input program.
    result = build.build(sources=[source],
                         options=options,
                         alt_lib_path=test_temp_dir)
    if result.errors:
        raise CompileError(result.errors)

    errors = Errors()
    modules = genops.build_ir(
        [result.files['__main__']], result.graph, result.types,
        genops.Mapper({'__main__': None}),
        compiler_options, errors)
    assert errors.num_errors == 0

    module = list(modules.values())[0]
    return module.functions
コード例 #2
0
ファイル: emitmodule.py プロジェクト: popzxc/mypy
def compile_modules_to_c(
    result: BuildResult,
    compiler_options: CompilerOptions,
    errors: Errors,
    groups: Groups,
) -> Tuple[ModuleIRs, List[FileContents]]:
    """Compile Python module(s) to the source of Python C extension modules.

    This generates the source code for the "shared library" module
    for each group. The shim modules are generated in mypyc.build.
    Each shared library module provides, for each module in its group,
    a PyCapsule containing an initialization function.
    Additionally, it provides a capsule containing an export table of
    pointers to all of the group's functions and static variables.

    Arguments:
        result: The BuildResult from the mypy front-end
        compiler_options: The compilation options
        errors: Where to report any errors encountered
        groups: The groups that we are compiling. See documentation of Groups type above.
        ops: Optionally, where to dump stringified ops for debugging.

    Returns the IR of the modules and a list containing the generated files for each group.
    """
    # Construct a map from modules to what group they belong to
    group_map = {
        source.module: lib_name
        for group, lib_name in groups for source in group
    }
    mapper = genops.Mapper(group_map)

    modules = compile_modules_to_ir(result, mapper, compiler_options, errors)
    ctext = compile_ir_to_c(groups, modules, result, mapper, compiler_options)

    if errors.num_errors == 0:
        write_cache(modules, result, group_map, ctext)

    return modules, [ctext[name] for _, name in groups]
コード例 #3
0
def compile_modules_to_c(
    result: BuildResult,
    compiler_options: CompilerOptions,
    errors: Errors,
    groups: Groups,
) -> Tuple[ModuleIRs, List[FileContents]]:
    """Compile Python module(s) to the source of Python C extension modules.

    This generates the source code for the "shared library" module
    for each group. The shim modules are generated in mypyc.build.
    Each shared library module provides, for each module in its group,
    a PyCapsule containing an initialization function.
    Additionally, it provides a capsule containing an export table of
    pointers to all of the group's functions and static variables.

    Arguments:
        result: The BuildResult from the mypy front-end
        compiler_options: The compilation options
        errors: Where to report any errors encountered
        groups: The groups that we are compiling. See documentation of Groups type above.
        ops: Optionally, where to dump stringified ops for debugging.

    Returns the IR of the modules and a list containing the generated files for each group.
    """
    module_names = [
        source.module for group_sources, _ in groups
        for source in group_sources
    ]
    file_nodes = [result.files[name] for name in module_names]

    # Construct a map from modules to what group they belong to
    group_map = {}
    for group, lib_name in groups:
        for source in group:
            group_map[source.module] = lib_name

    # Generate basic IR, with missing exception and refcount handling.
    mapper = genops.Mapper(group_map)
    modules = genops.build_ir(file_nodes, result.graph, result.types, mapper,
                              compiler_options, errors)
    if errors.num_errors > 0:
        return modules, []
    # Insert uninit checks.
    for module in modules.values():
        for fn in module.functions:
            insert_uninit_checks(fn)
    # Insert exception handling.
    for module in modules.values():
        for fn in module.functions:
            insert_exception_handling(fn)
    # Insert refcount handling.
    for module in modules.values():
        for fn in module.functions:
            insert_ref_count_opcodes(fn)

    source_paths = {
        module_name: result.files[module_name].path
        for module_name in module_names
    }

    names = NameGenerator([[source.module for source in sources]
                           for sources, _ in groups])

    # Generate C code for each compilation group. Each group will be
    # compiled into a separate extension module.
    ctext = []
    for group_sources, group_name in groups:
        group_modules = [(source.module, modules[source.module])
                         for source in group_sources]
        literals = mapper.literals[group_name]
        generator = GroupGenerator(literals, group_modules, source_paths,
                                   group_name, group_map, names,
                                   compiler_options.multi_file)
        ctext.append(generator.generate_c_for_modules())
    return modules, ctext