|
| 1 | +from typing import List |
| 2 | + |
1 | 3 | from clang import cindex |
2 | 4 |
|
3 | 5 | from . import scope_list |
4 | 6 |
|
5 | 7 |
|
6 | | -def fn_arg_type(cursor: cindex.Cursor): |
| 8 | +def fn_arg_type(cursor: cindex.Cursor) -> List[str]: |
7 | 9 | return [param.type.get_canonical().spelling for param in cursor.get_arguments()] |
8 | 10 |
|
9 | 11 |
|
10 | | -def fn_ret_type(cursor: cindex.Cursor): |
| 12 | +def fn_ret_type(cursor: cindex.Cursor) -> str: |
11 | 13 | return cursor.result_type.get_canonical().spelling |
12 | 14 |
|
13 | 15 |
|
14 | | -def get_fn_pointer_type(cursor: cindex.Cursor): |
| 16 | +def _get_fn_pointer_type(cursor: cindex.Cursor) -> str: |
15 | 17 | if cursor.kind == cindex.CursorKind.CXX_METHOD and not cursor.is_static_method(): |
16 | 18 | const_mark = "const" if cursor.is_const_method() else "" |
17 | 19 | return f"{fn_ret_type(cursor)} ({scope_list.get_full_qualified_name(cursor.semantic_parent)}::*)({','.join(fn_arg_type(cursor))}) {const_mark}" |
18 | 20 | else: |
19 | 21 | return f"{fn_ret_type(cursor)} (*)({','.join(fn_arg_type(cursor))})" |
| 22 | + |
| 23 | + |
| 24 | +def get_fn_pointer(cursor: cindex.Cursor) -> str: |
| 25 | + pointer = f"&{scope_list.get_full_qualified_name(cursor)}" |
| 26 | + method_pointer_type = _get_fn_pointer_type(cursor) |
| 27 | + return f"static_cast<{method_pointer_type}>({pointer})" |
| 28 | + |
| 29 | + |
| 30 | +__wrapped_db = set() |
| 31 | + |
| 32 | + |
| 33 | +def get_wrapped_types(): |
| 34 | + return __wrapped_db |
| 35 | + |
| 36 | + |
| 37 | +def warp_type(type: cindex.Type, param_name: str): |
| 38 | + type = type.get_canonical() |
| 39 | + ret = None, param_name |
| 40 | + if type.kind == cindex.TypeKind.POINTER: |
| 41 | + pointee = type.get_pointee().get_canonical() |
| 42 | + # if pointee is a pointer, warp it |
| 43 | + if pointee.kind in [cindex.TypeKind.POINTER]: |
| 44 | + ret = f"pybind11_weaver::PointerWrapper<{type.spelling}>", param_name |
| 45 | + if pointee.kind in [cindex.TypeKind.FUNCTIONPROTO]: |
| 46 | + ret = f"std::function<{pointee.spelling}>", f"{param_name}.target<{pointee.spelling}>()" |
| 47 | + # if pointee is a incompelete type, warp it |
| 48 | + pointee_decl = pointee.get_declaration() |
| 49 | + if pointee_decl.kind in [cindex.CursorKind.STRUCT_DECL, |
| 50 | + cindex.CursorKind.CLASS_DECL] and not pointee_decl.is_definition(): |
| 51 | + ret = f"pybind11_weaver::PointerWrapper<{type.spelling}>", param_name |
| 52 | + if ret[0] is not None: |
| 53 | + __wrapped_db.add(type.spelling) |
| 54 | + return ret |
| 55 | + |
| 56 | + |
| 57 | +__warpper_template = """[]({params}){{ |
| 58 | + return {ret_expr}; |
| 59 | +}}""" |
| 60 | + |
| 61 | + |
| 62 | +def get_fn_wrapper(cursor: cindex.Cursor): |
| 63 | + params = [] |
| 64 | + if cursor.kind != cindex.CursorKind.FUNCTION_DECL: |
| 65 | + params.append(f"{cursor.semantic_parent.spelling}& self") |
| 66 | + args = [] |
| 67 | + warp = False |
| 68 | + for param in cursor.get_arguments(): |
| 69 | + param_t = param.type.get_canonical() |
| 70 | + param_spelling = param.spelling |
| 71 | + if param_spelling == "": |
| 72 | + param_spelling = "arg" + str(len(args)) |
| 73 | + warp_t, arg_use = warp_type(param_t, param_spelling) |
| 74 | + if warp_t: |
| 75 | + warp = True |
| 76 | + params.append(f"{warp_t} {param_spelling}") |
| 77 | + else: |
| 78 | + params.append(f"{param_t.spelling} {param_spelling}") |
| 79 | + args.append(arg_use) |
| 80 | + ret_t = cursor.result_type.get_canonical() |
| 81 | + ret_expr = f"{cursor.spelling}({','.join(args)})" |
| 82 | + if cursor.kind != cindex.CursorKind.FUNCTION_DECL: |
| 83 | + ret_expr = f"self.{ret_expr}" |
| 84 | + warp_t, _ = warp_type(ret_t, "") |
| 85 | + if warp_t: |
| 86 | + warp = True |
| 87 | + ret_expr = f"{warp_t}({ret_expr})" |
| 88 | + if warp: |
| 89 | + return __warpper_template.format(params=','.join(params), ret_expr=ret_expr) |
| 90 | + else: |
| 91 | + return None |
| 92 | + |
| 93 | + |
| 94 | +def get_fn_value_expr(cursor: cindex.Cursor) -> str: |
| 95 | + wrapper = get_fn_wrapper(cursor) |
| 96 | + if wrapper: |
| 97 | + return wrapper |
| 98 | + else: |
| 99 | + return get_fn_pointer(cursor) |
0 commit comments