Skip to content

Integer type not generated properly when it's a template argument #75

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

Closed
upsuper opened this issue Oct 7, 2016 · 8 comments
Closed

Comments

@upsuper
Copy link
Contributor

upsuper commented Oct 7, 2016

If I have a type nsTArray<uint16_t>, it is expected that I would get nsTArray<u16> from bindgen. However, I get nsTArray<::std::os::raw::c_ushort> instead.

It seems the uint16_t-to-u16 conversion is done in type_from_named inside codegen mod. I suspect that the uint16_t in template argument list is converted to unsigned short in parsing phase, and consequently codegen has nothing to do with it.

@emilio
Copy link
Contributor

emilio commented Oct 7, 2016

Hmm... It's not that. It's a combination of two bugs.

First, clang represents that as an Unexposed type. Since it has a canonical type which is an exposed one (nsTArray<unsigned short>) we pick the first branch in the match in Type::from_clang_ty.

@emilio
Copy link
Contributor

emilio commented Oct 7, 2016

The other one is that even with that fixed, the function clang_Type_getTemplateArgumentAsType returns UShort to us :(

@upsuper
Copy link
Contributor Author

upsuper commented Oct 8, 2016

Is it possible to treat those types as opaque types and force clang to hand over them to us?

@emilio
Copy link
Contributor

emilio commented Oct 8, 2016

I don't think so. I suspect the spelling of the opaque type would be
correct, though.

We can move the scanning of "known" typedefs over to parsing without too
much effort (though I'm not extremely happy about it). That would fix
the integer types for example, but not other types of typedefs.

On Fri, Oct 07, 2016 at 05:00:43PM -0700, Xidorn Quan wrote:

Is it possible to treat those types as opaque types and force clang to hand over them to us?

You are receiving this because you commented.
Reply to this email directly or view it on GitHub:
#75 (comment)

@upsuper
Copy link
Contributor Author

upsuper commented Oct 10, 2016

What do you mean by "move the scanning ... over"?

If we cannot hack into libclang to get the names right, we can probably do some other hacks.

One idea occurs to me is to build a fake stdint.h, and make clang to use it first, and in that fake header, we define the integer types as opaque types, so that clang wouldn't replace them with the builtin types. That works for u?int{8,16,32,64,ptr}_t, but doesn't work for size_t and ptrdiff_t, because they are from builtin includes, and clang would ignore any further definition of them.

Another idea is that, we inject a normal header via -include, with content like the following

#include <stdint.h>
enum __builtin_uint16_t : uint16_t;
#define uint16_t __builtin_uint16_t

This would work reliably for all those types as far as it doesn't try to compile any calculation.

But these hacks may break some type traits, I guess...

@upsuper
Copy link
Contributor Author

upsuper commented Oct 10, 2016

Yeah, it would definitely break any integer-related traits. I guess it's probably impossible to have a solution which doesn't break any trait while makes clang report those types.

@upsuper
Copy link
Contributor Author

upsuper commented Oct 10, 2016

Probably I should just make some opaque struct and teach bindgen to treat them as nsTArray in the Rust side.

@emilio
Copy link
Contributor

emilio commented May 10, 2017

This is fixed by clang 4.0+, so the only regression pending fixing from that change is the default template parameters one (#585). Luckily that doesn't affect stylo.

There's no way to get the proper parameter from libclang <4.0, so there's nothing left to be done here.

@emilio emilio closed this as completed May 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants