An idea sort of hit me while I was thinking about the discussion in this thread. What if there’s a special C define that changes the behavior of translate-c such that it’d embed meta information into the function name? Say we have the follow in a header file:
void foo(const char* bytes, size_t bytes_len);
If we import the header in this manner:
const c = @cImport({
@cDefine("__ZIG_NAME_MANGLING", {});
@cInclude("foo.h");
});
Then translate-c would give us something like this:
export const @"foo:\"void\" bytes:\"const char*\" bytes_len:\"size_t\"" = @extern(
fn (?[*] const u8, bytes_len: usize) callconv(.c) void,
.{ .name = "foo" },
});
That would gives us the missing information at comptime required for automated function transform. If we have the names of the types and the names of the arguments then we can establish naming conventions that we can reliable act upon. For instance, if a size_t argument ends in “[name]_len” and the preceding argument is “[name]”, then these two arguments should be merged into a slice. Or if a pointer type’s name ends in “_maybe”, then it should be handled as an optional.
This is similar to C++ name-mangling, except that we wouldn’t be mangling the actual names as they exist in the .so
file. The metadata would come from the header file.
I think this can open up a lot of possibilities all without any change to the language itself. All we’re doing is making translate-c behave differently when a special constant is defined.