marlersoft / zigwin32gen

Generates Complete Zig bindings for Win32. See https://github.com/marlersoft/zigwin32 for the bindings themselves.
108 stars 17 forks source link

Additional metadata for pointer union types #9

Open marler8997 opened 3 years ago

marler8997 commented 3 years ago

This is a follow up to the discussion here: https://github.com/microsoft/win32metadata/issues/623

The issue is that some win32 pointer parameters accept more than just pointers, but Zig verifies pointer alignment which prevents these non-pointer values from being passed. For example, calling CreateWindowEx with the return value of RegisterClass for lpClassName will trigger a pointer alignment issue when performing an alignCast from ATOM to LPCSTR.

It was also discussed that changing these pointer parameters to a union type can break the ABI since some ABI's treat pointers and structure types differently, even if the structure itself is made up of a single pointer value.

So given these constraints, the solution I came up with for Zig is to maintain additional metadata that indicates which of these pointer parameters or fields can accept other non-pointer values and add align(1) to those pointer types. If another projection would also like this additional metadata, then this extra data could be upstreamed and turned into a new attribute in the Microsoft supplied metadata. Note that since this is a possibility, I should make the implementation as similar to an attribute as I can.

marler8997 commented 8 months ago

initial support added here: https://github.com/marlersoft/zigwin32gen/commit/7c22826159cd83c3597bc7e9992d5571e8d7e1c6