From 37054b795862dc3466cec5e7cd0ccc605b70c6c1 Mon Sep 17 00:00:00 2001 From: Peterino2 Date: Thu, 22 Jan 2026 18:39:02 -0800 Subject: [PATCH] Fix: Properly parse function pointer fields in structs (SDL_IOStreamInterface) - Fixed parseStructField to correctly extract field names from function pointer declarations - Pattern: RetType (SDLCALL *field_name)(params) now correctly identifies 'field_name' - Prevents function pointer types from causing recursion in convertType - Function pointer types temporarily converted to ?*const anyopaque placeholder - SDL_IOStreamInterface now parses correctly with proper field names (size, seek, read, write, flush, close) - Next step: implement full function pointer type conversion to Zig syntax --- lib/sdl3/parser/src/patterns.zig | 6 - lib/sdl3/parser/src/types.zig | 21 +- lib/sdl3/test-iostream.json | 734 ------------------------------- lib/sdl3/test.json | 1 - 4 files changed, 15 insertions(+), 747 deletions(-) delete mode 100644 lib/sdl3/test-iostream.json delete mode 100644 lib/sdl3/test.json diff --git a/lib/sdl3/parser/src/patterns.zig b/lib/sdl3/parser/src/patterns.zig index d141d34..fbb9467 100644 --- a/lib/sdl3/parser/src/patterns.zig +++ b/lib/sdl3/parser/src/patterns.zig @@ -655,7 +655,6 @@ pub const Scanner = struct { fn parseStructField(self: *Scanner, line: []const u8) !?FieldDecl { const trimmed = std.mem.trim(u8, line, " \t\r"); - std.debug.print("DEBUG parseStructField: trimmed='{s}'\n", .{trimmed}); if (trimmed.len == 0) return null; if (std.mem.startsWith(u8, trimmed, "//")) return null; if (std.mem.startsWith(u8, trimmed, "/*")) return null; @@ -678,17 +677,12 @@ pub const Scanner = struct { } } - std.debug.print("DEBUG parseStructField: field_part='{s}'\n", .{field_part}); - // Check for function pointer field: RetType (SDLCALL *field_name)(params) if (std.mem.indexOf(u8, field_part, "(SDLCALL *")) |sdlcall_pos| { - std.debug.print("DEBUG: Found SDLCALL function pointer in: {s}\n", .{field_part}); // Find the * after SDLCALL const after_sdlcall = field_part[sdlcall_pos + 10..]; // Skip "(SDLCALL *" - std.debug.print("DEBUG: after_sdlcall: {s}\n", .{after_sdlcall}); if (std.mem.indexOf(u8, after_sdlcall, ")")) |close_paren| { const field_name = std.mem.trim(u8, after_sdlcall[0..close_paren], " \t"); - std.debug.print("DEBUG: field_name extracted: {s}\n", .{field_name}); // The entire thing is the type (we'll convert to Zig function pointer syntax later) return FieldDecl{ diff --git a/lib/sdl3/parser/src/types.zig b/lib/sdl3/parser/src/types.zig index f76bef3..5f91d4a 100644 --- a/lib/sdl3/parser/src/types.zig +++ b/lib/sdl3/parser/src/types.zig @@ -6,9 +6,11 @@ const Allocator = std.mem.Allocator; pub fn convertType(c_type: []const u8, allocator: Allocator) ![]const u8 { const trimmed = std.mem.trim(u8, c_type, " \t"); - // Handle function pointers: RetType (SDLCALL *name)(params) -> *const fn(params) callconv(.C) RetType - if (std.mem.indexOf(u8, trimmed, "(SDLCALL *") orelse std.mem.indexOf(u8, trimmed, "(*")) |star_pos| { - return try convertFunctionPointerType(trimmed, allocator); + // Handle function pointers: For now, just return as placeholder until we implement full conversion + if (std.mem.indexOf(u8, trimmed, "(SDLCALL *") != null or std.mem.indexOf(u8, trimmed, "(*") != null) { + // TODO: Implement full function pointer conversion + // For now, return a placeholder type + return try std.fmt.allocPrint(allocator, "?*const anyopaque", .{}); } // Handle array types: "Uint8[2]" -> "[2]u8" @@ -163,8 +165,11 @@ fn convertFunctionPointerType(c_type: []const u8, allocator: Allocator) ![]const const params_str = std.mem.trim(u8, c_type[last_open_paren + 1 .. last_close_paren], " \t"); - // Convert return type - const zig_return = try convertType(return_type_str, allocator); + // Convert return type (but don't recursively convert function pointers) + const zig_return = if (std.mem.indexOf(u8, return_type_str, "(") != null) + try allocator.dupe(u8, return_type_str) + else + try convertType(return_type_str, allocator); defer allocator.free(zig_return); // Convert parameters @@ -196,7 +201,11 @@ fn convertFunctionPointerType(c_type: []const u8, allocator: Allocator) ![]const } } - const zig_param = try convertType(param_type, allocator); + // Don't recursively convert function pointers in params + const zig_param = if (std.mem.indexOf(u8, param_type, "(") != null) + try allocator.dupe(u8, param_type) + else + try convertType(param_type, allocator); try params_list.append(zig_param); } diff --git a/lib/sdl3/test-iostream.json b/lib/sdl3/test-iostream.json deleted file mode 100644 index dc163d8..0000000 --- a/lib/sdl3/test-iostream.json +++ /dev/null @@ -1,734 +0,0 @@ -{ - "header": "SDL_iostream.h", - "opaque_types": [ - { - "name": "SDL_IOStream" - } - ], - "typedefs": [], - "function_pointers": [], - "enums": [ - { - "name": "SDL_IOStatus", - "values": [] - }, - { - "name": "SDL_IOWhence", - "values": [] - } - ], - "structs": [ - { - "name": "SDL_IOStreamInterface", - "fields": [ - { - "name": "version", - "type": "Uint32" - }, - { - "name": "userdata", - "type": "Sint64 (SDLCALL *size)(void *" - }, - { - "name": "whence", - "type": "Sint64 (SDLCALL *seek)(void *userdata, Sint64 offset, SDL_IOWhence" - }, - { - "name": "status", - "type": "size_t (SDLCALL *read)(void *userdata, void *ptr, size_t size, SDL_IOStatus *" - }, - { - "name": "status", - "type": "size_t (SDLCALL *write)(void *userdata, const void *ptr, size_t size, SDL_IOStatus *" - }, - { - "name": "status", - "type": "bool (SDLCALL *flush)(void *userdata, SDL_IOStatus *" - }, - { - "name": "userdata", - "type": "bool (SDLCALL *close)(void *" - } - ] - } - ], - "unions": [], - "flags": [], - "functions": [ - { - "name": "SDL_IOFromFile", - "return_type": "SDL_IOStream *", - "parameters": [ - { - "name": "file", - "type": "const char *" - }, - { - "name": "mode", - "type": "const char *" - } - ] - }, - { - "name": "SDL_IOFromMem", - "return_type": "SDL_IOStream *", - "parameters": [ - { - "name": "mem", - "type": "void *" - }, - { - "name": "size", - "type": "size_t" - } - ] - }, - { - "name": "SDL_IOFromConstMem", - "return_type": "SDL_IOStream *", - "parameters": [ - { - "name": "mem", - "type": "const void *" - }, - { - "name": "size", - "type": "size_t" - } - ] - }, - { - "name": "SDL_IOFromDynamicMem", - "return_type": "SDL_IOStream *", - "parameters": [] - }, - { - "name": "SDL_OpenIO", - "return_type": "SDL_IOStream *", - "parameters": [ - { - "name": "iface", - "type": "const SDL_IOStreamInterface *" - }, - { - "name": "userdata", - "type": "void *" - } - ] - }, - { - "name": "SDL_CloseIO", - "return_type": "bool", - "parameters": [ - { - "name": "context", - "type": "SDL_IOStream *" - } - ] - }, - { - "name": "SDL_GetIOProperties", - "return_type": "SDL_PropertiesID", - "parameters": [ - { - "name": "context", - "type": "SDL_IOStream *" - } - ] - }, - { - "name": "SDL_GetIOStatus", - "return_type": "SDL_IOStatus", - "parameters": [ - { - "name": "context", - "type": "SDL_IOStream *" - } - ] - }, - { - "name": "SDL_GetIOSize", - "return_type": "Sint64", - "parameters": [ - { - "name": "context", - "type": "SDL_IOStream *" - } - ] - }, - { - "name": "SDL_SeekIO", - "return_type": "Sint64", - "parameters": [ - { - "name": "context", - "type": "SDL_IOStream *" - }, - { - "name": "offset", - "type": "Sint64" - }, - { - "name": "whence", - "type": "SDL_IOWhence" - } - ] - }, - { - "name": "SDL_TellIO", - "return_type": "Sint64", - "parameters": [ - { - "name": "context", - "type": "SDL_IOStream *" - } - ] - }, - { - "name": "SDL_ReadIO", - "return_type": "size_t", - "parameters": [ - { - "name": "context", - "type": "SDL_IOStream *" - }, - { - "name": "ptr", - "type": "void *" - }, - { - "name": "size", - "type": "size_t" - } - ] - }, - { - "name": "SDL_WriteIO", - "return_type": "size_t", - "parameters": [ - { - "name": "context", - "type": "SDL_IOStream *" - }, - { - "name": "ptr", - "type": "const void *" - }, - { - "name": "size", - "type": "size_t" - } - ] - }, - { - "name": "SDL_IOprintf", - "return_type": "size_t", - "parameters": [ - { - "name": "context", - "type": "SDL_IOStream *" - }, - { - "name": "fmt", - "type": "const char *" - }, - { - "name": "", - "type": "..." - } - ] - }, - { - "name": "SDL_IOvprintf", - "return_type": "size_t", - "parameters": [ - { - "name": "context", - "type": "SDL_IOStream *" - }, - { - "name": "fmt", - "type": "const char *" - }, - { - "name": "ap", - "type": "va_list" - } - ] - }, - { - "name": "SDL_FlushIO", - "return_type": "bool", - "parameters": [ - { - "name": "context", - "type": "SDL_IOStream *" - } - ] - }, - { - "name": "SDL_LoadFile_IO", - "return_type": "void *", - "parameters": [ - { - "name": "src", - "type": "SDL_IOStream *" - }, - { - "name": "datasize", - "type": "size_t *" - }, - { - "name": "closeio", - "type": "bool" - } - ] - }, - { - "name": "SDL_LoadFile", - "return_type": "void *", - "parameters": [ - { - "name": "file", - "type": "const char *" - }, - { - "name": "datasize", - "type": "size_t *" - } - ] - }, - { - "name": "SDL_SaveFile_IO", - "return_type": "bool", - "parameters": [ - { - "name": "src", - "type": "SDL_IOStream *" - }, - { - "name": "data", - "type": "const void *" - }, - { - "name": "datasize", - "type": "size_t" - }, - { - "name": "closeio", - "type": "bool" - } - ] - }, - { - "name": "SDL_SaveFile", - "return_type": "bool", - "parameters": [ - { - "name": "file", - "type": "const char *" - }, - { - "name": "data", - "type": "const void *" - }, - { - "name": "datasize", - "type": "size_t" - } - ] - }, - { - "name": "SDL_ReadU8", - "return_type": "bool", - "parameters": [ - { - "name": "src", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Uint8 *" - } - ] - }, - { - "name": "SDL_ReadS8", - "return_type": "bool", - "parameters": [ - { - "name": "src", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Sint8 *" - } - ] - }, - { - "name": "SDL_ReadU16LE", - "return_type": "bool", - "parameters": [ - { - "name": "src", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Uint16 *" - } - ] - }, - { - "name": "SDL_ReadS16LE", - "return_type": "bool", - "parameters": [ - { - "name": "src", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Sint16 *" - } - ] - }, - { - "name": "SDL_ReadU16BE", - "return_type": "bool", - "parameters": [ - { - "name": "src", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Uint16 *" - } - ] - }, - { - "name": "SDL_ReadS16BE", - "return_type": "bool", - "parameters": [ - { - "name": "src", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Sint16 *" - } - ] - }, - { - "name": "SDL_ReadU32LE", - "return_type": "bool", - "parameters": [ - { - "name": "src", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Uint32 *" - } - ] - }, - { - "name": "SDL_ReadS32LE", - "return_type": "bool", - "parameters": [ - { - "name": "src", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Sint32 *" - } - ] - }, - { - "name": "SDL_ReadU32BE", - "return_type": "bool", - "parameters": [ - { - "name": "src", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Uint32 *" - } - ] - }, - { - "name": "SDL_ReadS32BE", - "return_type": "bool", - "parameters": [ - { - "name": "src", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Sint32 *" - } - ] - }, - { - "name": "SDL_ReadU64LE", - "return_type": "bool", - "parameters": [ - { - "name": "src", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Uint64 *" - } - ] - }, - { - "name": "SDL_ReadS64LE", - "return_type": "bool", - "parameters": [ - { - "name": "src", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Sint64 *" - } - ] - }, - { - "name": "SDL_ReadU64BE", - "return_type": "bool", - "parameters": [ - { - "name": "src", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Uint64 *" - } - ] - }, - { - "name": "SDL_ReadS64BE", - "return_type": "bool", - "parameters": [ - { - "name": "src", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Sint64 *" - } - ] - }, - { - "name": "SDL_WriteU8", - "return_type": "bool", - "parameters": [ - { - "name": "dst", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Uint8" - } - ] - }, - { - "name": "SDL_WriteS8", - "return_type": "bool", - "parameters": [ - { - "name": "dst", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Sint8" - } - ] - }, - { - "name": "SDL_WriteU16LE", - "return_type": "bool", - "parameters": [ - { - "name": "dst", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Uint16" - } - ] - }, - { - "name": "SDL_WriteS16LE", - "return_type": "bool", - "parameters": [ - { - "name": "dst", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Sint16" - } - ] - }, - { - "name": "SDL_WriteU16BE", - "return_type": "bool", - "parameters": [ - { - "name": "dst", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Uint16" - } - ] - }, - { - "name": "SDL_WriteS16BE", - "return_type": "bool", - "parameters": [ - { - "name": "dst", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Sint16" - } - ] - }, - { - "name": "SDL_WriteU32LE", - "return_type": "bool", - "parameters": [ - { - "name": "dst", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Uint32" - } - ] - }, - { - "name": "SDL_WriteS32LE", - "return_type": "bool", - "parameters": [ - { - "name": "dst", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Sint32" - } - ] - }, - { - "name": "SDL_WriteU32BE", - "return_type": "bool", - "parameters": [ - { - "name": "dst", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Uint32" - } - ] - }, - { - "name": "SDL_WriteS32BE", - "return_type": "bool", - "parameters": [ - { - "name": "dst", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Sint32" - } - ] - }, - { - "name": "SDL_WriteU64LE", - "return_type": "bool", - "parameters": [ - { - "name": "dst", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Uint64" - } - ] - }, - { - "name": "SDL_WriteS64LE", - "return_type": "bool", - "parameters": [ - { - "name": "dst", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Sint64" - } - ] - }, - { - "name": "SDL_WriteU64BE", - "return_type": "bool", - "parameters": [ - { - "name": "dst", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Uint64" - } - ] - }, - { - "name": "SDL_WriteS64BE", - "return_type": "bool", - "parameters": [ - { - "name": "dst", - "type": "SDL_IOStream *" - }, - { - "name": "value", - "type": "Sint64" - } - ] - } - ] -} \ No newline at end of file diff --git a/lib/sdl3/test.json b/lib/sdl3/test.json deleted file mode 100644 index 1270c53..0000000 --- a/lib/sdl3/test.json +++ /dev/null @@ -1 +0,0 @@ -{ "samplers": 0, "storage_textures": 0, "storage_buffers": 1, "uniform_buffers": 0 }