anonymous struct types in C
Presently there is no way to access a member that is of an anonymous type in C code.
Consider the following example:
// foo.h
#include <stdbool.h>
struct _FooA {
bool b;
struct {
int i;
} s;
};
typedef struct _FooA FooA;
// foo.vapi
[CCode (cheader_filename = "foo.h")]
namespace Foo {
public struct AS {
int i;
}
public struct A {
bool b;
AS s;
}
}
// bar.vala
using Foo;
void main () {
var a = A ();
int field = a.s.i;
}
This produces the following (incorrect) C code:
/* bar.c generated by valac 0.54.2, the Vala compiler
* generated from bar.vala, do not modify */
#include "foo.h"
#include <string.h>
#include <glib.h>
static void _vala_main (void);
static void
_vala_main (void)
{
FooA a = {0};
gint field = 0;
FooA _tmp0_;
FooAS _tmp1_;
memset (&a, 0, sizeof (FooA));
_tmp0_ = a;
_tmp1_ = _tmp0_.s;
field = _tmp1_.i;
}
int
main (int argc,
char ** argv)
{
_vala_main ();
return 0;
}
One proposed idea is to introduce a [CCode (anonymous = true)]
attribute, which, when placed on the struct AS
will ensure its C type is never used, and instead either the temporary is elided or typeof()
is used to ensure the temporary has the appropriate type (note that we cannot simply generate an identical anonymous struct type before the temporary, because this results in a new type that is incompatible, see this).
Edited by Princeton Ferro