Re: [Vala] Incorrect C generated with union containing anonymous struct



On Thu, 2011-10-13 at 00:10 +0200, Jan-Jaap van der Geer wrote:
(Excuses if this appears twice, but after waiting half an hour my
first attempt still hasn't appeared...)

I have the following struct:

typedef union foobar foobar;

union foobar
   {  struct
      {  char *bar;
      }
      foo;
   };

This is represented in the vapi like this:

[CCode (cheader_filename = "struct.h")]
namespace StructTest
{
  [CCode (cname = "foo")]
  public struct Foo
  {
    [CCode (cname = "bar")]
    public unowned string bar;
  }

  [CCode (cname = "foobar")]
  public struct Foobar
  {
    [CCode (cname = "foo")]
    public unowned Foo foo;
  }
}

There are basically two ways to do this... what you usually see in vapis
is something like this:

public struct Foobar {
  [CCode (cname = "foo.bar")]
  public unowned string foo_bar;
}

The other option, which I haven't tested in a while but used to work
well enough, is something like:

[CCode (cname = "struct { char *bar; }")]
public struct Foo {
  public unowned string bar;
}

public struct Foobar {
  public Foo foo;
}


I use it like this:

    var fbar = Foobar();
    Memory.copy (fbar.foo.bar, "tralala", 10);

In Vala 0.13.1 this worked fine and the following code was
generated:

    foobar fbar = {0};
    memset (&fbar, 0, sizeof (foobar));
    memcpy (fbar.foo.bar, "tralala", (gsize) 10);

However, in version 0.13.2 and beyond the following code is
generated:

    foobar fbar = {0};
    foobar _tmp0_;
    foo _tmp1_;
    const gchar* _tmp2_;
    memset (&fbar, 0, sizeof (foobar));
    _tmp0_ = fbar;
    _tmp1_ = _tmp0_.foo;
    _tmp2_ = _tmp1_.bar;
    memcpy (_tmp2_, "tralala", (gsize) 10);

This fails since foo, the variable of _tmp1_ is undefined in C. I
could of course remove the [CCode (cname = "foo")] in the vapi but
that only changes the name to something else that is not known.

Is this a bug? Is there a workaround? Note that the C-code is
beyond my control, so workarounds involving changing the definition
of the struct in C will most probably not work...

No, it's not a bug. The fact that it used to work was a bit of a
fluke... there are situations where it never did (such as assigning
Foobar.foo to a variable).


-Evan




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]