Commit 47cc10b3 authored by Jürg Billeter's avatar Jürg Billeter Committed by Jürg Billeter

support arbitrary expressions as enum values, fixes bug 488387

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
parent 08e77adc
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
......
......@@ -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 \
......
......@@ -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) {
......
/* 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);
}
}
}
......@@ -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 ()));
}
}
......
......@@ -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);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment