This commit is contained in:
nub31
2025-10-17 14:45:55 +02:00
parent 36eae1a2a2
commit 9a8ecced83
10 changed files with 1783 additions and 285 deletions

22
bindings/generate.py Normal file → Executable file
View File

@@ -1,3 +1,5 @@
#!/usr/bin/env python3
import sys
import os
import clang.cindex
@@ -11,6 +13,8 @@ def map_type(clang_type: Type):
canonical.spelling.replace("const ", "")
.replace("volatile ", "")
.replace("restrict ", "")
.replace("struct ", "")
.replace("union ", "")
)
if kind == TypeKind.POINTER:
@@ -18,6 +22,17 @@ def map_type(clang_type: Type):
if pointee.kind == TypeKind.CHAR_S or pointee.kind == TypeKind.CHAR_U:
return "cstring"
if pointee.kind == TypeKind.FUNCTIONPROTO:
arg_types = []
for arg in pointee.get_canonical().argument_types():
arg_types.append(map_type(arg))
mapped_return = map_type(pointee.get_canonical().get_result())
args_str = ", ".join(arg_types)
return f"func({args_str}): {mapped_return}"
return f"^{map_type(pointee)}"
if kind == TypeKind.CONSTANTARRAY:
@@ -65,10 +80,7 @@ def map_type(clang_type: Type):
return "f64"
if kind == TypeKind.RECORD:
if not spelling.startswith("struct "):
raise Exception(f"Unknown custom type: {spelling}")
return spelling.replace("struct ", "")
return spelling
raise Exception(f"Unresolved type: {spelling}")
@@ -81,7 +93,7 @@ filename = sys.argv[1]
index = clang.cindex.Index.create()
tu = index.parse(filename, ["-x", "c", "-std=c23"])
tu = index.parse(filename, ["-x", "c", "-std=c23", "-I/usr/include"])
if tu.diagnostics:
for diag in tu.diagnostics: