ocaml-ctypes icon indicating copy to clipboard operation
ocaml-ctypes copied to clipboard

Cstubs_structs and anonymous structs/unions

Open andrewray opened this issue 8 years ago • 5 comments

While automatically converting various c-headers I get all sorts of anonymous structs(/unions). This is working somewhat with the libffi interface (modulo its various restrictions) but I am having some trouble with Cstubs_structs generation on anonymous fields.

I am not so sure that Cstubs_structs should actually be trying to determine information about anonymous structs, except through some named parent struct.

I believe this is somewhat related to a conversation in #249.

andrewray avatar Nov 02 '16 02:11 andrewray

Working with this example

struct na {
  int a;
  struct {
    int x;
  } b;
};

I have updated the cstubs structs example. In bindings_stubs_gen.c we get a couple of compile errors:

First,

  puts("  | Struct ({ tag = \"\"} as s'), \"x\" ->");
  printf("    let f = {ftype; fname; foffset = %zu} in \n",
        offsetof(struct , x));

which should be fixable in write_c by generating

        offsetof(struct na, b.x));

The second problem might be a bit harder to fix;

  puts("  | Struct ({ tag = \"\"; spec = Incomplete _ } as s') ->");
  printf("    s'.spec <- Complete { size = %zu; align = %zu }\n",
        sizeof(struct ), offsetof(struct { char c; struct  x; }, x));

The struct size we can get by declaring struct na na; and sizeof(na.b). I am not sure how to proceed with the alignment calculation, however. I'm not actually sure in what cases the alignment of the anonymous struct would be needed;

  • you might need to create one to initialize the parent struct? A conservative default might suffice here
  • or some internal machinary might use it in laying out the parent struct in which case it should be accurate. I note there is a non standard alignof operator in gcc.

andrewray avatar Nov 02 '16 11:11 andrewray

puts(" | Struct ({ tag = \"\"; spec = Incomplete _ } as s') ->");

This will require changing how we do things, since the generated pattern will match any anonymous struct, which won't work if there are two or more such structs in the same generated code.

yallop avatar Nov 02 '16 12:11 yallop

There's a partial fix in #467.

yallop avatar Nov 02 '16 14:11 yallop

Small update here - having looked through the generated code with Ctypes_structs I realised I could generate an equivalent myself with information from clang. For the time being, I am not too worried about this interface.

andrewray avatar Nov 03 '16 14:11 andrewray

Thanks for the update, @andrewray. It's good to hear that this isn't a blocker for ctypes_of_clang

There is some progress towards a fix in https://github.com/yallop/ocaml-ctypes/commit/1df83aee3bfc6f0e8b7618fc2d3ba5b7dc203a66, which generates correct C code for untagged struct members, but doesn't yet generate ML code that can disambiguate between two untagged structs.

yallop avatar Nov 03 '16 14:11 yallop