Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Dec 5, 2024
1 parent bbc5b72 commit 95ccd4e
Showing 1 changed file with 35 additions and 103 deletions.
138 changes: 35 additions & 103 deletions bindgen/gen_swift.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@

# NOTE: syntax for function results: "func_name.RESULT"
overrides = {
'func': 'func_',
'slog_func': 'fn',
'func': 'fn',
'sgl_error': 'sgl_get_error', # 'error' is reserved in Swift
'sgl_deg': 'sgl_as_degrees',
'sgl_rad': 'sgl_as_radians',
Expand Down Expand Up @@ -89,7 +90,7 @@
'double': 'Double',
'uintptr_t': 'UInt',
'intptr_t': 'Int',
'size_t': 'UInt'
'size_t': 'Int'
}

prim_defaults = {
Expand Down Expand Up @@ -211,7 +212,8 @@ def as_c_arg_type(arg_type, prefix):
elif is_prim_type(arg_type):
return as_swift_prim_type(arg_type)
elif is_struct_type(arg_type):
return as_swift_struct_type(arg_type, prefix)
return "OpaquePointer?"
# return as_swift_struct_type(arg_type, prefix)
elif is_enum_type(arg_type):
return as_swift_enum_type(arg_type, prefix)
elif util.is_void_ptr(arg_type):
Expand All @@ -221,11 +223,12 @@ def as_c_arg_type(arg_type, prefix):
elif util.is_string_ptr(arg_type):
return "UnsafePointer<CChar>?"
elif is_const_struct_ptr(arg_type):
return f"{as_swift_struct_type(util.extract_ptr_type(arg_type), prefix)}"
return "UnsafeRawPointer?"
# return f"UnsafePointer<{as_swift_struct_type(util.extract_ptr_type(arg_type), prefix)}>"
elif is_prim_ptr(arg_type):
return f"{as_swift_prim_type(util.extract_ptr_type(arg_type))}"
return f"UnsafeMutablePointer<{as_swift_prim_type(util.extract_ptr_type(arg_type))}>"
elif is_const_prim_ptr(arg_type):
return f"let {as_swift_prim_type(util.extract_ptr_type(arg_type))}"
return f"UnsafePointer<{as_swift_prim_type(util.extract_ptr_type(arg_type))}>"
else:
sys.exit(f"Error as_c_arg_type(): {arg_type}")

Expand All @@ -240,7 +243,8 @@ def as_swift_arg_type(arg_prefix, arg_type, prefix):
elif is_prim_type(arg_type):
return pre + as_swift_prim_type(arg_type)
elif is_struct_type(arg_type):
return pre + as_swift_struct_type(arg_type, prefix)
return pre + "OpaquePointer?"
# return pre + as_swift_struct_type(arg_type, prefix)
elif is_enum_type(arg_type):
return pre + as_swift_enum_type(arg_type, prefix)
elif util.is_void_ptr(arg_type):
Expand All @@ -250,7 +254,8 @@ def as_swift_arg_type(arg_prefix, arg_type, prefix):
elif util.is_string_ptr(arg_type):
return pre + "UnsafePointer<CChar>?"
elif is_const_struct_ptr(arg_type):
return pre + f"{as_swift_struct_type(util.extract_ptr_type(arg_type), prefix)}"
# return pre + f"OpaquePointer?"
return pre + f"UnsafePointer<{as_swift_struct_type(util.extract_ptr_type(arg_type), prefix)}>?"
elif is_prim_ptr(arg_type):
return pre + f"UnsafeMutablePointer<{as_swift_prim_type(util.extract_ptr_type(arg_type))}>?"
elif is_const_prim_ptr(arg_type):
Expand Down Expand Up @@ -298,7 +303,7 @@ def funcdecl_args_c(decl, prefix):
s += ", "
param_name = param_decl['name']
param_type = check_override(f'{func_name}.{param_name}', default=param_decl['type'])
s += as_c_arg_type(param_type, prefix)
s += f'_: {as_c_arg_type(param_type, prefix)}'
return s

def funcdecl_args_swift(decl, prefix):
Expand Down Expand Up @@ -345,7 +350,7 @@ def gen_struct(decl, prefix):
elif util.is_void_ptr(field_type):
l(f" var {field_name}: UnsafeMutableRawPointer? = nil")
elif is_const_prim_ptr(field_type):
l(f" let {field_name}: {as_swift_prim_type(util.extract_ptr_type(field_type))} = nil")
l(f" var {field_name}: {as_swift_prim_type(util.extract_ptr_type(field_type))} = nil")
elif util.is_func_ptr(field_type):
l(f" var {field_name}: (@convention(c) ({funcptr_args_c(field_type, prefix)}) -> {funcptr_result_c(field_type)})? = nil")
elif util.is_1d_array_type(field_type):
Expand All @@ -367,7 +372,7 @@ def gen_struct(decl, prefix):
t1 = f"{swift_type}"
l(f" var {field_name}: {t0} = Array(repeating: {t1}({def_val}), count: {array_sizes[0]})")
elif util.is_const_void_ptr(array_type):
l(f" {field_name}: [UnsafeRawPointer?] = Array(repeating: nil, count: {array_sizes[0]})")
l(f" var {field_name}: [UnsafeRawPointer?] = Array(repeating: nil, count: {array_sizes[0]})")
else:
sys.exit(f"ERROR gen_struct: array {field_name}: {field_type} => {array_type} [{array_sizes[0]}]")
elif util.is_2d_array_type(field_type):
Expand All @@ -381,8 +386,8 @@ def gen_struct(decl, prefix):
def_val = ""
else:
sys.exit(f"ERROR gen_struct is_2d_array_type: {array_type}")
t0 = f"[{array_sizes[0]}][{array_sizes[1]}]{swift_type}"
l(f" {field_name}: {t0} = [_][{array_sizes[1]}]{swift_type}{{[_]{swift_type}{{{def_val}}} ** {array_sizes[1]}}} ** {array_sizes[0]}")
t0 = f"{swift_type}"
l(f" var {field_name}: [[{t0}]] = Array(repeating: Array(repeating: {t0}({def_val}), count: {array_sizes[1]}), count: {array_sizes[0]})")
else:
sys.exit(f"ERROR gen_struct: {field_name}: {field_type}")
l("}")
Expand All @@ -394,7 +399,9 @@ def gen_consts(decl, prefix):

def gen_enum(decl, prefix):
enum_name = check_override(decl['name'])
l(f"enum {as_swift_enum_type(enum_name, prefix)} : Int32 {{")
l('')
l('@objc')
l(f"enum {as_swift_enum_type(enum_name, prefix)} : CUnsignedInt {{")
for item in decl['items']:
item_name = as_enum_item_name(check_override(item['name']))
if item_name != "Force_u32":
Expand All @@ -406,10 +413,12 @@ def gen_enum(decl, prefix):

def gen_func_c(decl, prefix):
l(f"@_extern(c, \"{decl['name']}\")")
if(funcdecl_result_c(decl, prefix) != "void"):
l(f"func {decl['name']}({funcdecl_args_c(decl, prefix)}) -> {funcdecl_result_c(decl, prefix)}")
c_res_type = funcdecl_result_c(decl, prefix)
if(c_res_type != "void"):
l(f"func {decl['name']}({funcdecl_args_c(decl, prefix)}) -> {c_res_type}")
else:
l(f"func {decl['name']}({funcdecl_args_c(decl, prefix)})")
l('')

def gen_func_swift(decl, prefix):
c_func_name = decl['name']
Expand All @@ -419,11 +428,11 @@ def gen_func_swift(decl, prefix):
l(f"typealias {swift_func_name} = {c_func_name}")
else:
swift_res_type = funcdecl_result_swift(decl, prefix)
l(f"func {swift_func_name}({funcdecl_args_swift(decl, prefix)}) {swift_res_type} {{")
if is_swift_string(swift_res_type):
# special case: convert C string to Swift string slice
s = f" return cStrToSwift({c_func_name}("
elif swift_res_type != 'void':
if funcdecl_result_c(decl, prefix) != 'void':
l(f"func {swift_func_name}({funcdecl_args_swift(decl, prefix)}) -> {swift_res_type} {{")
else:
l(f"func {swift_func_name}({funcdecl_args_swift(decl, prefix)}) {{")
if swift_res_type != 'void':
s = f" return {c_func_name}("
else:
s = f" {c_func_name}("
Expand All @@ -433,13 +442,13 @@ def gen_func_swift(decl, prefix):
arg_name = param_decl['name']
arg_type = param_decl['type']
if is_const_struct_ptr(arg_type):
s += f"&{arg_name}"
s += f"UnsafePointer({arg_name})"
elif util.is_string_ptr(arg_type):
s += f"{arg_name}"
elif is_struct_type(arg_type):
s += f"{arg_name}"
else:
s += arg_name
if is_swift_string(swift_res_type):
s += ")"
s += ")"
l(s)
l("}")
Expand All @@ -459,87 +468,10 @@ def pre_parse(inp):
enum_items[enum_name].append(as_enum_item_name(item['name']))

def gen_imports(inp, dep_prefixes):
# l('const builtin = @import("builtin");')
for dep_prefix in dep_prefixes:
dep_module_name = module_names[dep_prefix]
# l(f'const {dep_prefix[:-1]} = @import("{dep_module_name}.swift");')
l('')
pass

def gen_helpers(inp):
l('// helper function to convert a C string to a Swift string slice')
# l('fn cStrToSwift(c_str: [*c]const u8) [:0]const u8 {')
# l(' return @import("std").mem.span(c_str);')
# l('}')
# if inp['prefix'] in ['sg_', 'sdtx_', 'sshape_', 'sfetch_']:
# l('// helper function to convert "anything" to a Range struct')
# l('pub fn asRange(val: anytype) Range {')
# l(' const type_info = @typeInfo(@TypeOf(val));')
# l(' // FIXME: naming convention change between 0.13 and 0.14-dev')
# l(' if (@hasField(@TypeOf(type_info), "Pointer")) {')
# l(' switch (type_info) {')
# l(' .Pointer => {')
# l(' switch (type_info.Pointer.size) {')
# l(' .One => return .{ .ptr = val, .size = @sizeOf(type_info.Pointer.child) },')
# l(' .Slice => return .{ .ptr = val.ptr, .size = @sizeOf(type_info.Pointer.child) * val.len },')
# l(' else => @compileError("FIXME: Pointer type!"),')
# l(' }')
# l(' },')
# l(' .Struct, .Array => {')
# l(' @compileError("Structs and arrays must be passed as pointers to asRange");')
# l(' },')
# l(' else => {')
# l(' @compileError("Cannot convert to range!");')
# l(' },')
# l(' }')
# l(' } else {')
# l(' switch (type_info) {')
# l(' .pointer => {')
# l(' switch (type_info.pointer.size) {')
# l(' .One => return .{ .ptr = val, .size = @sizeOf(type_info.pointer.child) },')
# l(' .Slice => return .{ .ptr = val.ptr, .size = @sizeOf(type_info.pointer.child) * val.len },')
# l(' else => @compileError("FIXME: Pointer type!"),')
# l(' }')
# l(' },')
# l(' .@"struct", .array => {')
# l(' @compileError("Structs and arrays must be passed as pointers to asRange");')
# l(' },')
# l(' else => {')
# l(' @compileError("Cannot convert to range!");')
# l(' },')
# l(' }')
# l(' }')
# l('}')
# l('')
# if inp['prefix'] == 'sdtx_':
# l('// std.fmt compatible Writer')
# l('pub const Writer = struct {')
# l(' pub const Error = error{};')
# l(' pub fn writeAll(self: Writer, bytes: []const u8) Error!void {')
# l(' _ = self;')
# l(' for (bytes) |byte| {')
# l(' putc(byte);')
# l(' }')
# l(' }')
# l(' pub fn writeByteNTimes(self: Writer, byte: u8, n: usize) Error!void {')
# l(' _ = self;')
# l(' var i: u64 = 0;')
# l(' while (i < n) : (i += 1) {')
# l(' putc(byte);')
# l(' }')
# l(' }')
# l(' pub fn writeBytesNTimes(self: Writer, bytes: []const u8, n: usize) Error!void {')
# l(' var i: usize = 0;')
# l(' while (i < n) : (i += 1) {')
# l(' try self.writeAll(bytes);')
# l(' }')
# l(' }')
# l('};')
# l('// std.fmt-style formatted print')
# l('pub fn print(comptime fmt: anytype, args: anytype) void {')
# l(' const writer: Writer = .{};')
# l(' @import("std").fmt.format(writer, fmt, args) catch {};')
# l('}')
# l('')
pass

def gen_module(inp, dep_prefixes):
l('// machine generated, do not edit')
Expand Down

0 comments on commit 95ccd4e

Please sign in to comment.