+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
valaccodeenum.c \
valaccodeenum.h \
valaccodeenum.vala \
+ valaccodeenumvalue.c \
+ valaccodeenumvalue.h \
+ valaccodeenumvalue.vala \
valaccodeexpression.c \
valaccodeexpression.h \
valaccodeexpression.vala \
valaccodedostatement.h \
valaccodeemptystatement.h \
valaccodeenum.h \
+ valaccodeenumvalue.h \
valaccodeexpression.h \
valaccodeexpressionstatement.h \
valaccodeformalparameter.h \
*/
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) {
}
* @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) {
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) {
--- /dev/null
+/* 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);
+ }
+ }
+}
}
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) {
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 ()));
}
}
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 ();
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);
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);
}