This commit is contained in:
nub31
2025-10-20 18:32:27 +02:00
parent 78441549c7
commit 5be9f122e9
5 changed files with 28 additions and 379 deletions

View File

@@ -19,6 +19,11 @@ def map_type(clang_type: Type):
if kind == TypeKind.POINTER:
pointee = canonical.get_pointee()
if pointee.kind == TypeKind.RECORD:
decl = pointee.get_declaration()
if not decl.is_definition():
return "^void"
if pointee.kind == TypeKind.CHAR_S or pointee.kind == TypeKind.CHAR_U:
return "cstring"
@@ -103,6 +108,8 @@ if tu.diagnostics:
print(f'module "{os.path.basename(filename).split(".")[0]}"')
print()
seen_structs = []
for cursor in tu.cursor.walk_preorder():
if cursor.location.file and cursor.location.file.name != filename:
continue
@@ -122,14 +129,20 @@ for cursor in tu.cursor.walk_preorder():
print(f'export extern "{name}" func {name}({params_str}): {return_type}')
elif cursor.kind == CursorKind.STRUCT_DECL:
name = cursor.spelling
print(f"export struct {name}")
print("{")
for field in cursor.get_children():
if field.kind == CursorKind.FIELD_DECL:
field_name = field.spelling
field_type = map_type(field.type)
print(f" {field_name}: {field_type}")
else:
raise Exception(f"Unsupported child of struct: {field.spelling}")
print("}")
if cursor.get_usr() in seen_structs:
continue
seen_structs.append(cursor.get_usr())
if cursor.is_definition():
name = cursor.spelling
print(f"export struct {name}")
print("{")
for field in cursor.get_children():
if field.kind == CursorKind.FIELD_DECL:
field_name = field.spelling
field_type = map_type(field.type)
print(f" {field_name}: {field_type}")
else:
raise Exception(f"Unsupported child of struct: {field.spelling}")
print("}")