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

cmd/cgo: map sized integer types to C equivalents #29878

Open
phst opened this Issue Jan 22, 2019 · 3 comments

Comments

Projects
None yet
4 participants
@phst
Copy link

phst commented Jan 22, 2019

(Removing the template since this is an enhancement proposal)

The type mappings at https://golang.org/cmd/cgo/#hdr-Go_references_to_C don't state which Go types the sized C integer types int8_t etc. are mapped to. In C, these types have the same requirements as the corresponding Go types: they are not allowed to have padding bits and have to use two's complement representation, see https://en.cppreference.com/w/c/types/integer. Therefore, these types are guaranteed to have the same domain and representation as the corresponding Go types. So I propose to always represent these types with each other and formally document that fact. It would reduce the need for casts and would make it more obvious that there's no chance of data loss.

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

ianlancetaylor commented Jan 22, 2019

Fine with me.

@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Jan 22, 2019

phst added a commit to phst/go that referenced this issue Jan 23, 2019

cmd/cgo: use C exact-width integer types to represent Go types
The exact-width integer types are required to use two’s complement
representation and may not have padding bits, cf. §7.20.1.1/1 in the C11
standard or https://en.cppreference.com/w/c/types/integer.  This ensures that
they have the same domain and representation as the corresponding Go types.

Fixes golang#29878
@gopherbot

This comment has been minimized.

Copy link

gopherbot commented Jan 23, 2019

Change https://golang.org/cl/159258 mentions this issue: cmd/cgo: use C exact-width integer types to represent Go types

@dpinela

This comment has been minimized.

Copy link
Contributor

dpinela commented Jan 23, 2019

I'm looking into this, is updating the (*typeConv).Type method in src/cmd/cgo/gcc.go sufficient to implement the equivalence? A quick experiment seems to show that it is.

edit: ninja'd. (And the answer turns out to be no.)

phst added a commit to phst/go that referenced this issue Jan 24, 2019

cmd/cgo: use C exact-width integer types to represent Go types
The exact-width integer types are required to use two’s complement
representation and may not have padding bits, cf. §7.20.1.1/1 in the C11
standard or https://en.cppreference.com/w/c/types/integer.  This ensures that
they have the same domain and representation as the corresponding Go types.

Fixes golang#29878
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment