-
Notifications
You must be signed in to change notification settings - Fork 18
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
Preliminary support for C arrays #76
base: master
Are you sure you want to change the base?
Conversation
I don't think we need that, Slices are fine. They're easy to get into an array if needed, otherwise bindgen should prefer lower overhead. Could be an opt-in feature however. Good stuff as always, cheers! |
About
How did Also it seems the PR is already quite large, so I'm calling it done once I add the documentation. General methods could wait a bit longer. (Most C array arguments in practice are just |
Putting this PR on hiatus because there is one major problem: slices can only refer to binding types even when the element type is wrapped. For example: struct Inner {
char a;
};
struct Outer {
Inner b[4];
Inner *c[5];
}; module Test
lib Binding
alias Inner = Void
end
class Inner
@unwrap : Binding::Inner*
def initialize(unwrap : Binding::Inner*)
end
end
class Outer
def b() : Slice(Binding::Inner)
# Slice(Void) not allowed
# copying Inner's structure might work
end
def c() : Slice(Binding::Inner*)
# ::Slice#[] returns a raw pointer; it won't call Inner#initialize
end
end
end One possible solution is to return a custom |
That sounds reasonable, still dang, didn't think about that either. I think that indeed, a custom |
That's it for now, Bindgen will currently reject arrays that are not built-in types (more specifically getter methods are omitted, and |
Could you resolve the conflict? LGTM after that 👍 |
I'll try to resolve the conflicts here during this weekend; @HertzDevil if you have any comments/notes re. that, please share. Thanks! |
Adds support for C array types, i.e.
int [4][3][2]
. It is not to be confused withstd::array
.StaticArray
members inlib struct
s generated bycopy_structure: true
. Multi-dimensional C arrays are also supported. VLAs are rejected by SanityCheck, because Crystal doesn't support them either.Slice
(read-only if the array fields areconst
). There will be no corresponding setters.Optional features:
Slice
if the bounds are known, andPointer
otherwise.int *x[4]
) and a pointer to an array (int (*x)[4]
). This patch assumes the former as it is way more common in C code than the latter, although in C++int (&x)[4]
would translate to the latter. The two can be differentiated by treating every array / pointer layer as a pseudo-generic type, similar toBindgen::Parser::Type#template
. The downside is that each layer creates a new type.const
pointers and pointers-to-const
as well.Array
s, provided they only contain other supported constants (Int | Float | String | Bool
).This patch also fixes what I believe to be a bug where CopyStructs converts
void **
fields to a plainvoid *
.