[vala] Return NULL in to_string() for invalid enum values



commit ba0881e981db557345c551546a432f527ff5f787
Author: Luca Bruno <lethalman88 gmail com>
Date:   Mon Mar 8 21:48:52 2010 +0100

    Return NULL in to_string() for invalid enum values
    
    Fixes bug 612141.

 codegen/valaccodebasemodule.vala |    2 ++
 codegen/valagtypemodule.vala     |   12 +++++++++---
 2 files changed, 11 insertions(+), 3 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index c7c6f81..72b4aa2 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -190,6 +190,7 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 	public TypeSymbol gptrarray_type;
 	public TypeSymbol gthreadpool_type;
 	public DataType gquark_type;
+	public DataType genumvalue_type;
 	public Struct gvalue_type;
 	public Struct mutex_type;
 	public TypeSymbol type_module_type;
@@ -332,6 +333,7 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 			gthreadpool_type = (TypeSymbol) glib_ns.scope.lookup ("ThreadPool");
 
 			gquark_type = new IntegerType ((Struct) glib_ns.scope.lookup ("Quark"));
+			genumvalue_type = new ObjectType ((Class) glib_ns.scope.lookup ("EnumValue"));
 			gvalue_type = (Struct) glib_ns.scope.lookup ("Value");
 			mutex_type = (Struct) glib_ns.scope.lookup ("StaticRecMutex");
 
diff --git a/codegen/valagtypemodule.vala b/codegen/valagtypemodule.vala
index 5a028b5..a919e51 100644
--- a/codegen/valagtypemodule.vala
+++ b/codegen/valagtypemodule.vala
@@ -1934,13 +1934,19 @@ internal class Vala.GTypeModule : GErrorModule {
 			return;
 		}
 
+		var ccomma = new CCodeCommaExpression ();
+		var temp_var = get_temp_variable (genumvalue_type, false, expr, false);
+		temp_vars.insert (0, temp_var);
+
 		var class_ref = new CCodeFunctionCall (new CCodeIdentifier ("g_type_class_ref"));
 		class_ref.add_argument (new CCodeIdentifier (ma.inner.value_type.get_type_id ()));
-
 		var get_value = new CCodeFunctionCall (new CCodeIdentifier ("g_enum_get_value"));
 		get_value.add_argument (class_ref);
 		get_value.add_argument ((CCodeExpression) get_ccodenode (((MemberAccess) expr.call).inner));
-		var value_name = new CCodeMemberAccess.pointer (get_value, "value_name");
-		expr.ccodenode = value_name;
+
+		ccomma.append_expression (new CCodeAssignment (get_variable_cexpression (temp_var.name), get_value));
+		var is_null_value = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, get_variable_cexpression (temp_var.name), new CCodeIdentifier ("NULL"));
+		ccomma.append_expression (new CCodeConditionalExpression (is_null_value, new CCodeMemberAccess.pointer (get_variable_cexpression (temp_var.name), "value_name"), new CCodeIdentifier ("NULL")));
+		expr.ccodenode = ccomma;
 	}
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]