From: Juerg Billeter Date: Sat, 27 Oct 2007 19:35:22 +0000 (+0000) Subject: support arbitrary expressions as enum values, fixes bug 488387 X-Git-Tag: VALA_0_1_5~52 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=47cc10b38cdfb338d86aa341f04c27088da5e6fc;p=thirdparty%2Fvala.git support arbitrary expressions as enum values, fixes bug 488387 2007-10-27 Juerg Billeter * ccode/Makefile.am, ccode/valaccodeenum.vala, ccode/valaccodeenumvalue.vala, gobject/valaccodegenerator.vala, gobject/valaccodegeneratorclass.vala: support arbitrary expressions as enum values, fixes bug 488387 svn path=/trunk/; revision=669 --- diff --git a/ChangeLog b/ChangeLog index 09a48d09d..76ea5b71f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2007-10-27 Jürg Billeter + + * ccode/Makefile.am, ccode/valaccodeenum.vala, + ccode/valaccodeenumvalue.vala, gobject/valaccodegenerator.vala, + gobject/valaccodegeneratorclass.vala: support arbitrary expressions as + enum values, fixes bug 488387 + 2007-10-27 Jürg Billeter * gobject/valaccodegeneratorsourcefile.vala: never write typedefs for diff --git a/ccode/Makefile.am b/ccode/Makefile.am index 95443ea7a..8162dee66 100644 --- a/ccode/Makefile.am +++ b/ccode/Makefile.am @@ -64,6 +64,9 @@ libvalaccode_la_SOURCES = \ valaccodeenum.c \ valaccodeenum.h \ valaccodeenum.vala \ + valaccodeenumvalue.c \ + valaccodeenumvalue.h \ + valaccodeenumvalue.vala \ valaccodeexpression.c \ valaccodeexpression.h \ valaccodeexpression.vala \ @@ -181,6 +184,7 @@ ccodeinclude_HEADERS = \ valaccodedostatement.h \ valaccodeemptystatement.h \ valaccodeenum.h \ + valaccodeenumvalue.h \ valaccodeexpression.h \ valaccodeexpressionstatement.h \ valaccodeformalparameter.h \ diff --git a/ccode/valaccodeenum.vala b/ccode/valaccodeenum.vala index 0ab08de05..ac50ed01f 100644 --- a/ccode/valaccodeenum.vala +++ b/ccode/valaccodeenum.vala @@ -32,7 +32,7 @@ public class Vala.CCodeEnum : CCodeNode { */ public string name { get; set; } - private Gee.List values = new ArrayList (); + private Gee.List values = new ArrayList (); public CCodeEnum (construct string name = null) { } @@ -43,12 +43,8 @@ public class Vala.CCodeEnum : CCodeNode { * @param name enum value name * @param value optional numerical value */ - public void add_value (string! name, string value = null) { - if (value == null) { - values.add (name); - } else { - values.add ("%s = %s".printf (name, value)); - } + public void add_value (CCodeEnumValue! value) { + values.add (value); } public override void write (CCodeWriter! writer) { @@ -58,13 +54,13 @@ public class Vala.CCodeEnum : CCodeNode { writer.write_string ("enum "); writer.write_begin_block (); bool first = true; - foreach (string value in values) { + foreach (CCodeEnumValue value in values) { if (!first) { writer.write_string (","); writer.write_newline (); } writer.write_indent (); - writer.write_string (value); + value.write (writer); first = false; } if (!first) { diff --git a/ccode/valaccodeenumvalue.vala b/ccode/valaccodeenumvalue.vala new file mode 100644 index 000000000..94f6e5bd4 --- /dev/null +++ b/ccode/valaccodeenumvalue.vala @@ -0,0 +1,50 @@ +/* valaccodeenumvalue.vala + * + * Copyright (C) 2007 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 + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Author: + * Jürg Billeter + */ + +using GLib; +using Gee; + +/** + * Represents an enum value in the C code. + */ +public class Vala.CCodeEnumValue : CCodeNode { + /** + * The name of this enum value. + */ + public string! name { get; set; } + + /** + * The numerical representation of this enum value. + */ + public CCodeExpression value { get; set; } + + public CCodeEnumValue (construct string! name, construct CCodeExpression value = null) { + } + + public override void write (CCodeWriter! writer) { + writer.write_string (name); + if (value != null) { + writer.write_string (" = "); + value.write (writer); + } + } +} diff --git a/gobject/valaccodegenerator.vala b/gobject/valaccodegenerator.vala index 1ea658df7..34ea1f9d1 100644 --- a/gobject/valaccodegenerator.vala +++ b/gobject/valaccodegenerator.vala @@ -326,14 +326,12 @@ public class Vala.CCodeGenerator : CodeGenerator { } public override void visit_enum_value (EnumValue! ev) { - string val; - if (ev.value is LiteralExpression) { - var lit = ((LiteralExpression) ev.value).literal; - if (lit is IntegerLiteral) { - val = ((IntegerLiteral) lit).value; - } + if (ev.value == null) { + cenum.add_value (new CCodeEnumValue (ev.get_cname ())); + } else { + ev.value.accept (this); + cenum.add_value (new CCodeEnumValue (ev.get_cname (), (CCodeExpression) ev.value.ccodenode)); } - cenum.add_value (ev.get_cname (), val); } public override void visit_callback (Callback! cb) { @@ -523,7 +521,7 @@ public class Vala.CCodeGenerator : CodeGenerator { next_temp_var_id = old_next_temp_var_id; if (prop.parent_symbol is Class) { - prop_enum.add_value (prop.get_upper_case_cname (), null); + prop_enum.add_value (new CCodeEnumValue (prop.get_upper_case_cname ())); } } diff --git a/gobject/valaccodegeneratorclass.vala b/gobject/valaccodegeneratorclass.vala index 315ef7390..1c92e967e 100644 --- a/gobject/valaccodegeneratorclass.vala +++ b/gobject/valaccodegeneratorclass.vala @@ -54,7 +54,7 @@ public class Vala.CCodeGenerator { type_struct = new CCodeStruct ("_%sClass".printf (cl.get_cname ())); instance_priv_struct = new CCodeStruct ("_%sPrivate".printf (cl.get_cname ())); prop_enum = new CCodeEnum (); - prop_enum.add_value ("%s_DUMMY_PROPERTY".printf (cl.get_upper_case_cname (null)), null); + prop_enum.add_value (new CCodeEnumValue ("%s_DUMMY_PROPERTY".printf (cl.get_upper_case_cname (null)))); class_init_fragment = new CCodeFragment (); instance_init_fragment = new CCodeFragment (); instance_dispose_fragment = new CCodeFragment (); @@ -369,7 +369,7 @@ public class Vala.CCodeGenerator { cspec.add_argument (new CCodeConstant ("G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_WRITABLE")); cinst.add_argument (cspec); init_block.add_statement (new CCodeExpressionStatement (cinst)); - prop_enum.add_value (enum_value, null); + prop_enum.add_value (new CCodeEnumValue (enum_value)); instance_priv_struct.add_field ("GBoxedCopyFunc", func_name); @@ -387,7 +387,7 @@ public class Vala.CCodeGenerator { cspec.add_argument (new CCodeConstant ("G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_WRITABLE")); cinst.add_argument (cspec); init_block.add_statement (new CCodeExpressionStatement (cinst)); - prop_enum.add_value (enum_value, null); + prop_enum.add_value (new CCodeEnumValue (enum_value)); instance_priv_struct.add_field ("GDestroyNotify", func_name); }