Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

C integer types as aliases of zig integers #3757

Closed
daurnimator opened this issue Nov 24, 2019 · 3 comments
Closed

C integer types as aliases of zig integers #3757

daurnimator opened this issue Nov 24, 2019 · 3 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@daurnimator
Copy link
Contributor

Currently c_ushort != u16.

15:14:52 | andrewrk | because the C integer types have different bit widths based on targets
15:15:15 | andrewrk | it matters for the C ABI

Instead, I propose that e.g. c_ushort just becomes an alias for the relevant u16/u32/etc.

As @andrewrk notes, this could happen in the standard library itself

15:20:11 | andrewrk | which would be renamed to std.c.int

@daurnimator daurnimator added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Nov 24, 2019
@andrewrk andrewrk added this to the 0.6.0 milestone Nov 25, 2019
@andrewrk
Copy link
Member

I'm for this, as long as we can't think of any problems this will cause.

@andrewrk
Copy link
Member

Ah, I remember why we need C integer types now. See #875. It's for the purpose of exporting a C ABI compatible function.

export fn foo() c_ushort { return 123; }

is different than

export fn foo() u16 { return 123; }

Even though they always have the same bit count, one of them generates the C code:

unsigned short foo(void);

And the other:

uint16_t foo(void);

It also matters for compile error messages when dealing with translated C code, as well as a more general resilience toward accidents with ABIs and different targets - not because the way C integers work is better, but because when porting C code or otherwise using a C ABI that uses such types, trying to port them to types which don't match under all targets could be an easy mistake to make.

That being said, I'll leave this proposal open for consideration because it is possible to take a different strategy of generating .h files, at least we can consider what that would look like. I believe such a strategy would be related to multibuilds (#3028).

@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Nov 25, 2019
@daurnimator
Copy link
Contributor Author

If a zig file @cImports something that does:

#if ((INT_MAX >> 15) >> 15) >= 1
typedef int  i32
#else
typedef int  i16
#endif

Then unless we repeat the ifdefery back in the generated .h file, then we're not correct anyway.

@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 27, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

2 participants