Thanks for the response.

Firstly, there was a bug in my code, it should have been
  public unowned char SourceName[256];
so sorry about that, but the problem still exists.  I also forgot to mention 
that I am using vala 0.7.5.

I initially tried without the [SimpleType], but this generates code:
void test_run_main (char** args, int args_length1) {
        TestInitParams _tmp0_ = {0};
        TestInitParams params;
        params = (memset (&_tmp0_, 0, sizeof (TestInitParams)), _tmp0_);
        params = params;
}

Which works, but seems hugely inefficient as it is creating two versions of the 
structure on the stack when only one is needed.  This is why I started 
investigating use of SimpleType.

What I have is a simple structure that just needs this style of code:
        TestInitParams params;
        memset (&params, 0, sizeof (TestInitParams);

Is there any way I can get valac to produce code like this?

I will also review the bug you submitted and comment there if necessary.

Regards

Matt

-----Original Message-----
From: vala-list-boun...@gnome.org on behalf of Jan Hudec
Sent: Tue 18/08/2009 07:25
To: Spencer, Matthew
Cc: vala-list@gnome.org
Subject: Re: [Vala] wrong struct code generated
 
On Mon, 17 Aug 2009 18:03:17 +0100, Spencer, Matthew wrote:
> Using the following code:
> 
> vapi:
> 
> namespace Test {
> 
>     [SimpleType]

I don't think you want a [SimpleType] annotation here. IIRC that
annotation means the type is not a struct, but a typedef of int, float
or something. Passing by value is specified by the fact it is a struct.

(I did some .vapi last week and anything declared struct was passed by
value)

>     [CCode (cheader_filename="testStruct.h")]
>     public struct InitParams {
>          public unowned char SourceName;

It contains a single char? Shouldn't it say public unowned string
SourceName? (and it would be source_name in Vala convention)

>     }
> }
> 
> vala:
> using Test;
> 
> namespace TestRun {
>     public static void main(string[] args) {
>         InitParams params=InitParams();
>       params=params;
>     }
> }
> 
> I get the following c code generated:
> <snip>
> void test_run_main (char** args, int args_length1) {
>         TestInitParams params;
>         params = memset (0, sizeof (TestInitParams));
>         params = params;
> }
> </snip>

Independent of whether your .vapi is correct, vala should not generate
bogus code. I have already hit this with GLib.Pid (which is, by the way,
real SimpleType -- it is declared as struct in glib-2.0.vapi, but as
typedef to int in glib.h) and been pointed at bug
http://bugzilla.gnome.org/show_bug.cgi?id=578968

> This is surely incorrect as the wrong number of arguments are being passed to 
> memset.  I would have thought that 
>         params = memset (&params, 0, sizeof (TestInitParams));

No, it should not. The result of memset is irrelevant, so it should have
been just:
        memset (&params, 0, sizeof (TestInitParams));

> should have been generated.  Is this a bug, or is my vapi file incorrect?

Please, try without the [SimpleType] if it really is defined as struct
in C. If it still won't work then, please comment on the above bug, that
it does not work for real structs either.

If it won't work for you, give the struct a constructor. I think in vala
(unlike C#) can have a parameterless constructor, but you have to verify
that by trying.


Hm, does anybody know whether Vala supports struct initializer
expression? I.e. would it work to:

  TestInitParams params = { 'c', };

or

  TestInitParams params = TestInitParams () { 'c', };

(or some other combination with or without 'new' and with or without '()')

--------------------------------------------------------------------------------
                                                - Jan Hudec `Bulb' <b...@ucw.cz>
_______________________________________________
Vala-list mailing list
Vala-list@gnome.org
http://mail.gnome.org/mailman/listinfo/vala-list


**************************************************************************************
This message is confidential and intended only for the addressee. If you have 
received this message in error, please immediately notify the 
postmas...@nds.com and delete it from your system as well as any copies. The 
content of e-mails as well as traffic data may be monitored by NDS for 
employment and security purposes. To protect the environment please do not 
print this e-mail unless necessary.

NDS Limited. Registered Office: One London Road, Staines, Middlesex, TW18 4EX, 
United Kingdom. A company registered in England and Wales. Registered no. 
3080780. VAT no. GB 603 8808 40-00
**************************************************************************************
_______________________________________________
Vala-list mailing list
Vala-list@gnome.org
http://mail.gnome.org/mailman/listinfo/vala-list

Reply via email to