| 1 |
"""Intermediate representation of modules.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from mypyc.common import JsonDict |
| 6 |
from mypyc.ir.class_ir import ClassIR |
| 7 |
from mypyc.ir.deps import Capsule, Dependency, HeaderDep, SourceDep |
| 8 |
from mypyc.ir.func_ir import FuncDecl, FuncIR |
| 9 |
from mypyc.ir.ops import DeserMaps |
| 10 |
from mypyc.ir.rtypes import RType, deserialize_type |
| 11 |
|
| 12 |
|
| 13 |
class ModuleIR: |
| 14 |
"""Intermediate representation of a module.""" |
| 15 |
|
| 16 |
def __init__( |
| 17 |
self, |
| 18 |
fullname: str, |
| 19 |
imports: list[str], |
| 20 |
functions: list[FuncIR], |
| 21 |
classes: list[ClassIR], |
| 22 |
final_names: list[tuple[str, RType]], |
| 23 |
type_var_names: list[str], |
| 24 |
) -> None: |
| 25 |
self.fullname = fullname |
| 26 |
self.imports = imports.copy() |
| 27 |
self.functions = functions |
| 28 |
self.classes = classes |
| 29 |
self.final_names = final_names |
| 30 |
# Names of C statics used for Python 3.12 type variable objects. |
| 31 |
# These are only visible in the module that defined them, so no need |
| 32 |
# to serialize. |
| 33 |
self.type_var_names = type_var_names |
| 34 |
# Dependencies needed by the module (such as capsules or source files) |
| 35 |
self.dependencies: set[Dependency] = set() |
| 36 |
|
| 37 |
def serialize(self) -> JsonDict: |
| 38 |
# Serialize dependencies as a list of dicts with type information |
| 39 |
serialized_deps = [] |
| 40 |
for dep in sorted(self.dependencies, key=lambda d: (type(d).__name__, str(d))): |
| 41 |
if isinstance(dep, Capsule): |
| 42 |
serialized_deps.append({"type": "Capsule", "name": dep.name}) |
| 43 |
elif isinstance(dep, SourceDep): |
| 44 |
source_dep: JsonDict = { |
| 45 |
"type": "SourceDep", |
| 46 |
"path": dep.path, |
| 47 |
"include_dirs": dep.include_dirs, |
| 48 |
"internal": dep.internal, |
| 49 |
} |
| 50 |
serialized_deps.append(source_dep) |
| 51 |
elif isinstance(dep, HeaderDep): |
| 52 |
header_dep: JsonDict = { |
| 53 |
"type": "HeaderDep", |
| 54 |
"path": dep.path, |
| 55 |
"include_dirs": dep.include_dirs, |
| 56 |
"internal": dep.internal, |
| 57 |
} |
| 58 |
serialized_deps.append(header_dep) |
| 59 |
|
| 60 |
return { |
| 61 |
"fullname": self.fullname, |
| 62 |
"imports": self.imports, |
| 63 |
"functions": [f.serialize() for f in self.functions], |
| 64 |
"classes": [c.serialize() for c in self.classes], |
| 65 |
"final_names": [(k, t.serialize()) for k, t in self.final_names], |
| 66 |
"dependencies": serialized_deps, |
| 67 |
} |
| 68 |
|
| 69 |
@classmethod |
| 70 |
def deserialize(cls, data: JsonDict, ctx: DeserMaps) -> ModuleIR: |
| 71 |
module = ModuleIR( |
| 72 |
data["fullname"], |
| 73 |
data["imports"], |
| 74 |
[ctx.functions[FuncDecl.get_id_from_json(f)] for f in data["functions"]], |
| 75 |
[ClassIR.deserialize(c, ctx) for c in data["classes"]], |
| 76 |
[(k, deserialize_type(t, ctx)) for k, t in data["final_names"]], |
| 77 |
[], |
| 78 |
) |
| 79 |
|
| 80 |
# Deserialize dependencies |
| 81 |
deps: set[Dependency] = set() |
| 82 |
for dep_dict in data["dependencies"]: |
| 83 |
if dep_dict["type"] == "Capsule": |
| 84 |
deps.add(Capsule(dep_dict["name"])) |
| 85 |
elif dep_dict["type"] == "SourceDep": |
| 86 |
deps.add( |
| 87 |
SourceDep( |
| 88 |
dep_dict["path"], |
| 89 |
include_dirs=dep_dict["include_dirs"], |
| 90 |
internal=dep_dict["internal"], |
| 91 |
) |
| 92 |
) |
| 93 |
elif dep_dict["type"] == "HeaderDep": |
| 94 |
deps.add( |
| 95 |
HeaderDep( |
| 96 |
dep_dict["path"], |
| 97 |
include_dirs=dep_dict["include_dirs"], |
| 98 |
internal=dep_dict["internal"], |
| 99 |
) |
| 100 |
) |
| 101 |
module.dependencies = deps |
| 102 |
|
| 103 |
return module |
| 104 |
|
| 105 |
|
| 106 |
def deserialize_modules(data: dict[str, JsonDict], ctx: DeserMaps) -> dict[str, ModuleIR]: |
| 107 |
"""Deserialize a collection of modules. |
| 108 |
|
| 109 |
The modules can contain dependencies on each other. |
| 110 |
|
| 111 |
Arguments: |
| 112 |
data: A dict containing the modules to deserialize. |
| 113 |
ctx: The deserialization maps to use and to populate. |
| 114 |
They are populated with information from the deserialized |
| 115 |
modules and as a precondition must have been populated by |
| 116 |
deserializing any dependencies of the modules being deserialized |
| 117 |
(outside of dependencies between the modules themselves). |
| 118 |
|
| 119 |
Returns a map containing the deserialized modules. |
| 120 |
""" |
| 121 |
for mod in data.values(): |
| 122 |
# First create ClassIRs for every class so that we can construct types and whatnot |
| 123 |
for cls in mod["classes"]: |
| 124 |
ir = ClassIR(cls["name"], cls["module_name"]) |
| 125 |
assert ir.fullname not in ctx.classes, "Class %s already in map" % ir.fullname |
| 126 |
ctx.classes[ir.fullname] = ir |
| 127 |
|
| 128 |
for mod in data.values(): |
| 129 |
# Then deserialize all of the functions so that methods are available |
| 130 |
# to the class deserialization. |
| 131 |
for method in mod["functions"]: |
| 132 |
func = FuncIR.deserialize(method, ctx) |
| 133 |
assert func.decl.id not in ctx.functions, ( |
| 134 |
"Method %s already in map" % func.decl.fullname |
| 135 |
) |
| 136 |
ctx.functions[func.decl.id] = func |
| 137 |
|
| 138 |
return {k: ModuleIR.deserialize(v, ctx) for k, v in data.items()} |
| 139 |
|
| 140 |
|
| 141 |
# ModulesIRs should also always be an *OrderedDict*, but if we |
| 142 |
# declared it that way we would need to put it in quotes everywhere... |
| 143 |
ModuleIRs = dict[str, ModuleIR] |