|
| 1 | +from typing import Union, Set, List |
| 2 | + |
| 3 | +from pylibclang import cindex |
| 4 | + |
| 5 | +from pybind11_weaver.utils import common |
| 6 | + |
| 7 | +_tramp_method = """ |
| 8 | +#ifndef PYBIND11_DISABLE_OVERRIDE_{disable_mark} |
| 9 | + {ret_t} {method_name}({params}) {qualifier} override {{ |
| 10 | + using _PB11_WR_RET_TYPE = {ret_t}; |
| 11 | + using _PB11_WR_CONCREATE_TYPE = {concreate_ref}; |
| 12 | + {override_type}( |
| 13 | + _PB11_WR_RET_TYPE, |
| 14 | + _PB11_WR_CONCREATE_TYPE, |
| 15 | + {method_name}, |
| 16 | + {args} |
| 17 | + ); |
| 18 | + }} |
| 19 | +#endif // PYBIND11_DISABLE_OVERRIDE_{disable_mark} |
| 20 | +""" |
| 21 | + |
| 22 | +_trampoline = """ |
| 23 | +template<class = void> |
| 24 | +class PyTramp{python_name}{nest_level} : public {base_ref_name} {{ |
| 25 | +public: |
| 26 | + using _PB11_WR_BaseT = {base_ref_name}; |
| 27 | + using _PB11_WR_BaseT::_PB11_WR_BaseT; |
| 28 | + {decls} |
| 29 | + {tramp_methods} |
| 30 | +}}; |
| 31 | +""" |
| 32 | + |
| 33 | + |
| 34 | +class Virtuals: |
| 35 | + def __init__(self): |
| 36 | + self.virtuals: List[cindex.Cursor] = [] |
| 37 | + self.pure_virtuals: List[cindex.Cursor] = [] |
| 38 | + |
| 39 | + self.sigs: Set[str] = set() |
| 40 | + self.template_decls: List[str] = [] |
| 41 | + self.subs = dict() |
| 42 | + |
| 43 | + self.base: Union[Virtuals, None] = None |
| 44 | + |
| 45 | + def create_base(self): |
| 46 | + assert self.base is None |
| 47 | + self.base = Virtuals() |
| 48 | + self.base.sigs = self.sigs |
| 49 | + return self.base |
| 50 | + |
| 51 | + def is_empty(self): |
| 52 | + if self.base is not None: |
| 53 | + base_empty = self.base.is_empty() |
| 54 | + else: |
| 55 | + base_empty = True |
| 56 | + return base_empty and len(self.virtuals) == 0 and len(self.pure_virtuals) == 0 |
| 57 | + |
| 58 | + def add_pure_virtual(self, cursor: cindex.Cursor): |
| 59 | + if self._try_add_sig(cursor): |
| 60 | + self.pure_virtuals.append(cursor) |
| 61 | + |
| 62 | + def add_virtual(self, cursor: cindex.Cursor): |
| 63 | + if self._try_add_sig(cursor): |
| 64 | + self.virtuals.append(cursor) |
| 65 | + |
| 66 | + def force_add_sig(self, cursor: cindex.Cursor): |
| 67 | + self.sigs.add(self._get_sig(cursor)) |
| 68 | + |
| 69 | + def _get_sig(self, cursor: cindex.Cursor): |
| 70 | + ret_t = common.safe_type_reference(cursor.result_type, self.subs) |
| 71 | + arg_t = [common.safe_type_reference(arg.type, self.subs) for arg in cursor.get_arguments()] |
| 72 | + fn_name = cursor.spelling |
| 73 | + sig = f"{ret_t} {fn_name}({','.join(arg_t)})" |
| 74 | + return sig |
| 75 | + |
| 76 | + def _try_add_sig(self, cursor: cindex.Cursor): |
| 77 | + sig = self._get_sig(cursor) |
| 78 | + if sig in self.sigs: |
| 79 | + return False |
| 80 | + self.sigs.add(sig) |
| 81 | + return True |
| 82 | + |
| 83 | + |
| 84 | +class Trampoline: |
| 85 | + |
| 86 | + def __new__(cls, entity): |
| 87 | + cursor = entity.cursor |
| 88 | + if common.is_marked_final(entity.cursor): |
| 89 | + return None |
| 90 | + virt = Virtuals() |
| 91 | + cls.detect_all_virtual_methods(cursor, virt) |
| 92 | + if virt.is_empty(): |
| 93 | + return None |
| 94 | + obj = super().__new__(cls) |
| 95 | + obj._virt = virt |
| 96 | + return obj |
| 97 | + |
| 98 | + def __init__(self, entity): |
| 99 | + self.entity = entity |
| 100 | + |
| 101 | + def _get_method(self, cursor: cindex.Cursor, override_type: str, concreate_ref: str): |
| 102 | + ret_t = common.safe_type_reference(cursor.result_type) |
| 103 | + method_name = cursor.spelling |
| 104 | + params_t = [f"{common.safe_type_reference(p.type)}" for p in cursor.get_arguments()] |
| 105 | + args = [p.spelling if p.spelling != "" else f"arg{i}" for i, p in enumerate(cursor.get_arguments())] |
| 106 | + last_right_paren = cursor.type.spelling.rfind(")") |
| 107 | + qualifier = cursor.type.spelling[last_right_paren + 1:] |
| 108 | + return _tramp_method.format( |
| 109 | + disable_mark=common.type_python_name(concreate_ref + cursor.type.spelling), |
| 110 | + ret_t=ret_t, |
| 111 | + method_name=method_name, |
| 112 | + params=", ".join(f"{p_t} {a}" for p_t, a in zip(params_t, args)), |
| 113 | + qualifier=qualifier, |
| 114 | + override_type=override_type, |
| 115 | + concreate_ref=concreate_ref, |
| 116 | + args=", ".join(args) |
| 117 | + ) |
| 118 | + |
| 119 | + def get_virt_def(self, virt: Virtuals, nest_level: int) -> str: |
| 120 | + ret = "" |
| 121 | + python_name = self.entity.name |
| 122 | + concreate_ref = self.entity.reference_name() |
| 123 | + |
| 124 | + def get_nest_str(nest_level: int) -> str: |
| 125 | + return "" if nest_level == 0 else str(nest_level) |
| 126 | + |
| 127 | + if virt.base is not None and not virt.base.is_empty(): |
| 128 | + ret = self.get_virt_def(virt.base, nest_level + 1) |
| 129 | + base_ref_name = f"PyTramp{self.entity.name}{get_nest_str(nest_level + 1)}<>" |
| 130 | + else: |
| 131 | + base_ref_name = self.entity.reference_name() |
| 132 | + methods = [] |
| 133 | + for cursor in virt.virtuals: |
| 134 | + methods.append(self._get_method(cursor, "PYBIND11_OVERRIDE", concreate_ref)) |
| 135 | + for cursor in virt.pure_virtuals: |
| 136 | + methods.append(self._get_method(cursor, "PYBIND11_OVERRIDE_PURE", concreate_ref)) |
| 137 | + ret = ret + _trampoline.format( |
| 138 | + python_name=python_name, |
| 139 | + base_ref_name=base_ref_name, |
| 140 | + decls="\n".join(virt.template_decls), |
| 141 | + tramp_methods="\n".join(methods), |
| 142 | + nest_level=get_nest_str(nest_level) |
| 143 | + ) |
| 144 | + return ret |
| 145 | + |
| 146 | + def get_defs(self) -> str: |
| 147 | + return self.get_virt_def(self._virt, 0) |
| 148 | + |
| 149 | + def get_trampoline_cls_name(self): |
| 150 | + return f"PyTramp{self.entity.name}<>" |
| 151 | + |
| 152 | + @staticmethod |
| 153 | + def detect_all_virtual_methods(cursor: cindex.Cursor, to_update: Virtuals): |
| 154 | + cursor, decls, subs = common.get_def_cls_cursor(cursor) |
| 155 | + if decls is None: |
| 156 | + return |
| 157 | + to_update.template_decls = decls |
| 158 | + to_update.subs = subs |
| 159 | + base_found = None |
| 160 | + for c in cursor.get_children(): |
| 161 | + if c.kind == cindex.CursorKind.CXCursor_CXXMethod: |
| 162 | + if c.is_virtual_method(): |
| 163 | + if common.is_marked_final(c) or not common.could_member_accessed(c): |
| 164 | + to_update.force_add_sig(c) |
| 165 | + continue |
| 166 | + if not common.could_member_accessed(c): |
| 167 | + continue |
| 168 | + if c.is_pure_virtual_method(): |
| 169 | + to_update.add_pure_virtual(c) |
| 170 | + elif c.is_virtual_method(): |
| 171 | + to_update.add_virtual(c) |
| 172 | + # recurse into base class |
| 173 | + elif c.kind == cindex.CursorKind.CXCursor_CXXBaseSpecifier: |
| 174 | + assert base_found is None, "Multiple inheritance not supported" |
| 175 | + base_found = c.type.get_declaration() |
| 176 | + if base_found is not None: |
| 177 | + base_virt = to_update.create_base() |
| 178 | + Trampoline.detect_all_virtual_methods(base_found, base_virt) |
0 commit comments