Debian Codegraph @

mypyc/irbuild/vtable.py

raw
1 """Compute vtables of native (extension) classes."""
2
3 from __future__ import annotations
4
5 import itertools
6
7 from mypyc.ir.class_ir import ClassIR, VTableEntries, VTableMethod
8 from mypyc.sametype import is_same_method_signature
9
10
11 def compute_vtable(cls: ClassIR) -> None:
12 """Compute the vtable structure for a class."""
13 if cls.vtable is not None:
14 return
15
16 if not cls.is_generated:
17 cls.has_dict = any(x.inherits_python for x in cls.mro)
18
19 for t in cls.mro[1:]:
20 # Make sure all ancestors are processed first
21 compute_vtable(t)
22 # Merge attributes from traits into the class
23 if not t.is_trait:
24 continue
25 for name, typ in t.attributes.items():
26 if not cls.is_trait and not any(name in b.attributes for b in cls.base_mro):
27 cls.attributes[name] = typ
28
29 cls.vtable = {}
30 if cls.base:
31 assert cls.base.vtable is not None
32 cls.vtable.update(cls.base.vtable)
33 cls.vtable_entries = specialize_parent_vtable(cls, cls.base)
34
35 # Include the vtable from the parent classes, but handle method overrides.
36 entries = cls.vtable_entries
37
38 all_traits = [t for t in cls.mro if t.is_trait]
39
40 for t in [cls] + [t for t in all_traits if t is not cls]:
41 for fn in itertools.chain(t.methods.values()):
42 # TODO: don't generate a new entry when we overload without changing the type
43 if fn == cls.get_method(fn.name, prefer_method=True):
44 cls.vtable[fn.name] = len(entries)
45 # If the class contains a glue method referring to itself, that is a
46 # shadow glue method to support interpreted subclasses.
47 shadow = cls.glue_methods.get((cls, fn.name))
48 entries.append(VTableMethod(t, fn.name, fn, shadow))
49
50 # Compute vtables for all of the traits that the class implements
51 if not cls.is_trait:
52 for trait in all_traits:
53 compute_vtable(trait)
54 cls.trait_vtables[trait] = specialize_parent_vtable(cls, trait)
55
56
57 def specialize_parent_vtable(cls: ClassIR, parent: ClassIR) -> VTableEntries:
58 """Generate the part of a vtable corresponding to a parent class or trait"""
59 updated = []
60 for entry in parent.vtable_entries:
61 # Find the original method corresponding to this vtable entry.
62 # (This may not be the method in the entry, if it was overridden.)
63 orig_parent_method = entry.cls.get_method(entry.name, prefer_method=True)
64 assert orig_parent_method
65 method_cls = cls.get_method_and_class(entry.name, prefer_method=True)
66 if method_cls:
67 child_method, defining_cls = method_cls
68 # TODO: emit a wrapper for __init__ that raises or something
69 if (
70 is_same_method_signature(orig_parent_method.sig, child_method.sig)
71 or orig_parent_method.name == "__init__"
72 ):
73 entry = VTableMethod(entry.cls, entry.name, child_method, entry.shadow_method)
74 else:
75 entry = VTableMethod(
76 entry.cls,
77 entry.name,
78 defining_cls.glue_methods[(entry.cls, entry.name)],
79 entry.shadow_method,
80 )
81 updated.append(entry)
82 return updated