-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Imported C Structs with VLAs #8759
Comments
Also I called them VLAs because that's what Zig called them, but aren't these different? The GCC link I provided able calls them a couple of things, but one of them is "flexible array members", which also seems like what the C standard calls them. |
Another possible solution is for Zig to ignore the flexible array member. |
It's not pretty, but one approach that might work would be, given typedef struct {
size_t len;
int arr[];
} mystruct; translate to: pub const mystruct = extern struct {
len: usize,
}; (i.e. drop the VLA field since Then, given |
I think this would be a reasonable translation: pub const ACPI_PNP_DEVICE_ID_LIST = extern struct {
Count: u32 align(max(@alignOf(u32), @alignOf(ACPI_PNP_DEVICE_ID)),
ListSize: u32,
pub fn Ids(self: *@This()) [*]ACPI_PNP_DEVICE_ID {
const offset = comptime std.mem.alignForward(@offsetOf(@This(), "ListSize") + @sizeOf(u32), @alignOf(ACPI_PNP_DEVICE_ID));
return @ptrCast([*]ACPI_PNP_DEVICE_ID, @alignCast(@alignOf(ACPI_PNP_DEVICE_ID), @ptrCast([*]u8, self) + offset));
}
}; It's not sufficient to just drop the field because the VLA may have higher alignment than the rest of the struct, so that needs to be taken into account. |
Wait for ziglang/zig#8759 to be resolved to do anymore work on this?
Technically a duplicate of #4775 but lets keep this one open instead since the discussion is here. |
Wait for ziglang/zig#8759 to be resolved to do anymore work on this?
Somewhat related, I was wondering whether an empty array member might work for representing this. const S = extern struct {
data: u8,
flexible_array_member: [0]u64,
}; The answer is "kind of": The alignment of For EDIT: My bad, didn't realize the issue was closed. I doubt this quirk would be considered worth its own issue though. |
I'm currently using 0.8.0-dev.2192+65d279bc0 if that matters.
Problem:
I'd like to use these C structs from ACPICA in my Zig code. The following code is generated from it:
The warning seemed fair by itself when I first looked at this, but this apparently means that I can't use
ACPI_DEVICE_INFO
/struct_acpi_device_info
at all:Having a variable amount of data at the end of a data structure seems like a common pattern in data structures dealing with hardware. I've had to write code like this here for USB EHCI, among other places to access this kind of data from hardware. I'm fine writing code like that and it seems like I'm not the first person writing Zig to do so: #6008.
Possible Solutions?
I'm wondering if, for the sake of better compatibility with C, Zig could only demote the variable length array field itself to an opaque type and allow them at the end of the struct. This would mirror how VLAs act in C. It might also make previously mentioned situation of using this in normal Zig more readable and reduce the amount of pointer arithmetic, but I think that's a minor advantage if it's one at all. Or maybe this should just should be limited to
extern struct
s if it is better to isolate this behavior?Alternatively it would be better from my perspective as a user if Zig fully supported VLAs in structs, but I'm assuming that's not on the table because normal VLAs have been shot down before: #3952.
Also
The actual
cimport.zig
lines with the warning are:It's kinda weird the comment is following
ACPI_PNP_DEVICE_ID
, shouldn't there be a newline there or it should be followingstruct_acpi_pnp_device_id_list
, which is what it's actually talking about?The text was updated successfully, but these errors were encountered: