Support [CCode (type = "Foo")] to insert appropriate casts in generated C
authorJürg Billeter <j@bitron.ch>
Mon, 23 Jun 2008 13:46:49 +0000 (13:46 +0000)
committerJürg Billeter <juergbi@src.gnome.org>
Mon, 23 Jun 2008 13:46:49 +0000 (13:46 +0000)
2008-06-23  Jürg Billeter  <j@bitron.ch>

* vala/valaattribute.vala:
* vala/valafield.vala:
* vala/valainterfacewriter.vala:
* vala/valanamedargument.vala:
* vala/valastringliteral.vala:
* gobject/valaccodegenerator.vala:
* vapigen/valagidlparser.vala:

Support [CCode (type = "Foo")] to insert appropriate casts in
generated C Code

* vapi/packages/gtk+-2.0/:

Fix GtkActionEntry binding, fixes bug 526874

* vapi/gtk+-2.0.vapi: regenerated

svn path=/trunk/; revision=1638

ChangeLog
gobject/valaccodegenerator.vala
vala/valaattribute.vala
vala/valafield.vala
vala/valainterfacewriter.vala
vala/valanamedargument.vala
vala/valastringliteral.vala
vapi/gtk+-2.0.vapi
vapi/packages/gtk+-2.0/gtk+-2.0.metadata
vapigen/valagidlparser.vala

index 85a9824..22044b2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,24 @@
 2008-06-23  Jürg Billeter  <j@bitron.ch>
 
+       * vala/valaattribute.vala:
+       * vala/valafield.vala:
+       * vala/valainterfacewriter.vala:
+       * vala/valanamedargument.vala:
+       * vala/valastringliteral.vala:
+       * gobject/valaccodegenerator.vala:
+       * vapigen/valagidlparser.vala:
+
+       Support [CCode (type = "Foo")] to insert appropriate casts in
+       generated C Code
+
+       * vapi/packages/gtk+-2.0/:
+
+       Fix GtkActionEntry binding, fixes bug 526874
+
+       * vapi/gtk+-2.0.vapi: regenerated
+
+2008-06-23  Jürg Billeter  <j@bitron.ch>
+
        * vala/valagenieparser.vala:
        * vala/valanamespacereference.vala:
        * vala/valasourcefile.vala:
index 29ba37e..17dd245 100644 (file)
@@ -1257,11 +1257,42 @@ public class Vala.CCodeGenerator : CodeGenerator {
        public override void visit_initializer_list (InitializerList list) {
                list.accept_children (this);
 
-               var clist = new CCodeInitializerList ();
-               foreach (Expression expr in list.get_initializers ()) {
-                       clist.append ((CCodeExpression) expr.ccodenode);
+               if (list.target_type.data_type is Struct) {
+                       /* initializer is used as struct initializer */
+                       var st = (Struct) list.target_type.data_type;
+
+                       var clist = new CCodeInitializerList ();
+
+                       var field_it = st.get_fields ().iterator ();
+                       foreach (Expression expr in list.get_initializers ()) {
+                               Field field = null;
+                               while (field == null) {
+                                       field_it.next ();
+                                       field = field_it.get ();
+                                       if (field.binding != MemberBinding.INSTANCE) {
+                                               // we only initialize instance fields
+                                               field = null;
+                                       }
+                               }
+
+                               var cexpr = (CCodeExpression) expr.ccodenode;
+
+                               string ctype = field.get_ctype ();
+                               if (ctype != null) {
+                                       cexpr = new CCodeCastExpression (cexpr, ctype);
+                               }
+
+                               clist.append (cexpr);
+                       }
+
+                       list.ccodenode = clist;
+               } else {
+                       var clist = new CCodeInitializerList ();
+                       foreach (Expression expr in list.get_initializers ()) {
+                               clist.append ((CCodeExpression) expr.ccodenode);
+                       }
+                       list.ccodenode = clist;
                }
-               list.ccodenode = clist;
        }
 
        public LocalVariable get_temp_variable (DataType type, bool value_owned = true, CodeNode? node_reference = null) {
index cb0fd73..984decd 100644 (file)
@@ -44,7 +44,7 @@ public class Vala.Attribute : CodeNode {
         * @param source_reference reference to source code
         * @return                 newly created attribute
         */
-       public Attribute (string name, SourceReference? source_reference) {
+       public Attribute (string name, SourceReference? source_reference = null) {
                this.name = name;
                this.source_reference = source_reference;
        }
index 8595a64..f67c1b3 100644 (file)
@@ -179,4 +179,21 @@ public class Vala.Field : Member, Lockable {
                        field_type = new_type;
                }
        }
+
+       public string? get_ctype () {
+               var attr = get_attribute ("CCode");
+               if (attr == null) {
+                       return null;
+               }
+               return attr.get_string ("type");
+       }
+
+       public void set_ctype (string ctype) {
+               var attr = get_attribute ("CCode");
+               if (attr == null) {
+                       attr = new Attribute ("CCode");
+                       attributes.append (attr);
+               }
+               attr.add_argument (new NamedArgument ("type", new StringLiteral ("\"%s\"".printf (ctype))));
+       }
 }
index 97b4b88..510b525 100644 (file)
@@ -516,8 +516,9 @@ public class Vala.InterfaceWriter : CodeVisitor {
                }
 
                bool custom_cname = (f.get_cname () != f.get_default_cname ());
+               bool custom_ctype = (f.get_ctype () != null);
                bool custom_cheaders = (f.parent_symbol is Namespace);
-               if (custom_cname || custom_cheaders) {
+               if (custom_cname || custom_ctype || custom_cheaders) {
                        write_indent ();
                        write_string ("[CCode (");
 
@@ -525,11 +526,19 @@ public class Vala.InterfaceWriter : CodeVisitor {
                                write_string ("cname = \"%s\"".printf (f.get_cname ()));
                        }
 
-                       if (custom_cheaders) {
+                       if (custom_ctype) {
                                if (custom_cname) {
                                        write_string (", ");
                                }
 
+                               write_string ("type = \"%s\"".printf (f.get_ctype ()));
+                       }
+
+                       if (custom_cheaders) {
+                               if (custom_cname || custom_ctype) {
+                                       write_string (", ");
+                               }
+
                                bool first = true;
                                string cheaders;
                                foreach (string cheader in f.get_cheader_filenames ()) {
index 897b216..36181a4 100644 (file)
@@ -1,6 +1,6 @@
 /* valanamedargument.vala
  *
- * Copyright (C) 2006  Jürg Billeter
+ * Copyright (C) 2006-2008  Jürg Billeter
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -45,10 +45,10 @@ public class Vala.NamedArgument : CodeNode {
         * @param source reference to source code
         * @return       newly created named argument
         */
-       public NamedArgument (string _name, Expression arg, SourceReference source) {
-               name = _name;
-               argument = arg;
-               source_reference = source;
+       public NamedArgument (string name, Expression argument, SourceReference? source_reference = null) {
+               this.name = name;
+               this.argument = argument;
+               this.source_reference = source_reference;
        }
        
        public override void accept (CodeVisitor visitor) {
index 9d30b95..c6f666e 100644 (file)
@@ -38,9 +38,9 @@ public class Vala.StringLiteral : Literal {
         * @param source reference to source code
         * @return       newly created string literal
         */
-       public StringLiteral (string s, SourceReference source) {
-               value = s;
-               source_reference = source;
+       public StringLiteral (string value, SourceReference? source_reference = null) {
+               this.value = value;
+               this.source_reference = source_reference;
        }
 
        /**
index ff3efcc..5cb57a0 100644 (file)
@@ -6212,6 +6212,7 @@ namespace Gtk {
                public weak string label;
                public weak string accelerator;
                public weak string tooltip;
+               [CCode (type = "GCallback")]
                public weak Gtk.ActionCallback callback;
        }
        [CCode (cheader_filename = "gtk/gtk.h")]
index 66d264d..32a76c7 100644 (file)
@@ -18,7 +18,7 @@ gtk_action_new.tooltip nullable="1"
 gtk_action_new.stock_id nullable="1"
 GtkAction::activate has_emitter="1"
 GtkActionEntry is_value_type="1"
-GtkActionEntry.callback type_name="ActionCallback"
+GtkActionEntry.callback type_name="ActionCallback" ctype="GCallback"
 gtk_action_group_add_action_with_accel.accelerator nullable="1"
 gtk_action_group_add_actions.user_data hidden="0"
 gtk_action_group_add_actions_full.user_data hidden="0"
index 746c514..0d512a4 100644 (file)
@@ -1663,6 +1663,7 @@ public class Vala.GIdlParser : CodeVisitor {
                }
 
                string cheader_filename = null;
+               string ctype = null;
 
                var attributes = get_attributes ("%s.%s".printf (current_data_type.get_cname (), node.name));
                if (attributes != null) {
@@ -1693,6 +1694,8 @@ public class Vala.GIdlParser : CodeVisitor {
                                        }
                                } else if (nv[0] == "cheader_filename") {
                                        cheader_filename = eval (nv[1]);
+                               } else if (nv[0] == "ctype") {
+                                       ctype = eval (nv[1]);
                                }
                        }
                }
@@ -1718,6 +1721,10 @@ public class Vala.GIdlParser : CodeVisitor {
                        field.set_cname (node.name);
                }
 
+               if (ctype != null) {
+                       field.set_ctype (ctype);
+               }
+
                if (cheader_filename != null) {
                        field.add_cheader_filename (cheader_filename);
                }