]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
support arbitrary expressions as enum values, fixes bug 488387
authorJuerg Billeter <j@bitron.ch>
Sat, 27 Oct 2007 19:35:22 +0000 (19:35 +0000)
committerJürg Billeter <juergbi@src.gnome.org>
Sat, 27 Oct 2007 19:35:22 +0000 (19:35 +0000)
2007-10-27  Juerg Billeter  <j@bitron.ch>

* 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

ChangeLog
ccode/Makefile.am
ccode/valaccodeenum.vala
ccode/valaccodeenumvalue.vala [new file with mode: 0644]
gobject/valaccodegenerator.vala
gobject/valaccodegeneratorclass.vala

index 09a48d09decc7195f793e0b11ac752d36ef46603..76ea5b71f332519562b4509a4c629a444f4f60fc 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2007-10-27  Jürg Billeter  <j@bitron.ch>
+
+       * 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  <j@bitron.ch>
 
        * gobject/valaccodegeneratorsourcefile.vala: never write typedefs for
index 95443ea7a916cb12bc6352be362b5444fe38d6b1..8162dee66e46ee009a7219b5298d45f62cecd184 100644 (file)
@@ -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 \
index 0ab08de05cd2e2669a5aa6eb6a23bf17ce2a259c..ac50ed01fda7f2e6cea5e5aae510cb904fcddd2a 100644 (file)
@@ -32,7 +32,7 @@ public class Vala.CCodeEnum : CCodeNode {
         */
        public string name { get; set; }
        
-       private Gee.List<string> values = new ArrayList<string> ();
+       private Gee.List<CCodeEnumValue> values = new ArrayList<CCodeEnumValue> ();
        
        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 (file)
index 0000000..94f6e5b
--- /dev/null
@@ -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 <j@bitron.ch>
+ */
+
+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);
+               }
+       }
+}
index 1ea658df743de4eb15df5c908d9595fd77a9318d..34ea1f9d13cb44f3ea36f0aa9c57d7dcf9d09f39 100644 (file)
@@ -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 ()));
                }
        }
 
index 315ef73905e36edc728afe86dddf90690977290c..1c92e967ebf6a1081b66594a64a53278e986baf2 100644 (file)
@@ -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);
                        }