From: Jürg Billeter Date: Mon, 23 Jun 2008 13:46:49 +0000 (+0000) Subject: Support [CCode (type = "Foo")] to insert appropriate casts in generated C X-Git-Tag: VALA_0_3_4~15 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e9ecb4a3ef03ee0c8007b39fee9f1a890cd4a980;p=platform%2Fupstream%2Fvala.git Support [CCode (type = "Foo")] to insert appropriate casts in generated C 2008-06-23 Jürg Billeter * 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 --- diff --git a/ChangeLog b/ChangeLog index 85a9824..22044b2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,24 @@ 2008-06-23 Jürg Billeter + * 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 + * vala/valagenieparser.vala: * vala/valanamespacereference.vala: * vala/valasourcefile.vala: diff --git a/gobject/valaccodegenerator.vala b/gobject/valaccodegenerator.vala index 29ba37e..17dd245 100644 --- a/gobject/valaccodegenerator.vala +++ b/gobject/valaccodegenerator.vala @@ -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) { diff --git a/vala/valaattribute.vala b/vala/valaattribute.vala index cb0fd73..984decd 100644 --- a/vala/valaattribute.vala +++ b/vala/valaattribute.vala @@ -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; } diff --git a/vala/valafield.vala b/vala/valafield.vala index 8595a64..f67c1b3 100644 --- a/vala/valafield.vala +++ b/vala/valafield.vala @@ -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)))); + } } diff --git a/vala/valainterfacewriter.vala b/vala/valainterfacewriter.vala index 97b4b88..510b525 100644 --- a/vala/valainterfacewriter.vala +++ b/vala/valainterfacewriter.vala @@ -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 ()) { diff --git a/vala/valanamedargument.vala b/vala/valanamedargument.vala index 897b216..36181a4 100644 --- a/vala/valanamedargument.vala +++ b/vala/valanamedargument.vala @@ -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) { diff --git a/vala/valastringliteral.vala b/vala/valastringliteral.vala index 9d30b95..c6f666e 100644 --- a/vala/valastringliteral.vala +++ b/vala/valastringliteral.vala @@ -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; } /** diff --git a/vapi/gtk+-2.0.vapi b/vapi/gtk+-2.0.vapi index ff3efcc..5cb57a0 100644 --- a/vapi/gtk+-2.0.vapi +++ b/vapi/gtk+-2.0.vapi @@ -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")] diff --git a/vapi/packages/gtk+-2.0/gtk+-2.0.metadata b/vapi/packages/gtk+-2.0/gtk+-2.0.metadata index 66d264d..32a76c7 100644 --- a/vapi/packages/gtk+-2.0/gtk+-2.0.metadata +++ b/vapi/packages/gtk+-2.0/gtk+-2.0.metadata @@ -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" diff --git a/vapigen/valagidlparser.vala b/vapigen/valagidlparser.vala index 746c514..0d512a4 100644 --- a/vapigen/valagidlparser.vala +++ b/vapigen/valagidlparser.vala @@ -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); }